Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

High-level getter/setter for the YAML tree used for transferring comments #32

Merged
merged 7 commits into from
Jul 27, 2020

Conversation

twelho
Copy link
Contributor

@twelho twelho commented Jul 24, 2020

Implements high-level GetCommentSource and SetCommentSource functions for accessing the *yaml.RNode tree used internally for transferring comments. This access enables applications pulling in libgitops to do advanced operations while still preserving comment transfer abilities, they can e.g. re-parent the comment source tree (move it under a sub-node) to match changes in the object's structure.

cc @luxas, feedback would be appreciated on whether you think these are sensible/in the right place/integrated well enough with the rest of the comment handling.

twelho added 2 commits July 24, 2020 16:39
Signed-off-by: Dennis Marttinen <dennis@weave.works>
Signed-off-by: Dennis Marttinen <dennis@weave.works>
@twelho twelho requested a review from luxas July 24, 2020 13:47
Copy link
Contributor

@luxas luxas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Please make unit tests, and dog-food this function to the rest of the code.


// Cast the object to a metav1.Object to get access to annotations.
// If this fails, the given object does not support storing comments.
metaObj, ok := toMetaObject(obj)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For an other refactor, but we shall make a union interface between metav1.Object and runtime.Object in order to have better type-safety for these kinds of things.

// SetCommentSource sets the given YAML tree as the source for transferring comments for the given runtime.Object.
// This may be used externally to implement e.g. re-parenting of the comment source tree when moving structs around.
func SetCommentSource(obj runtime.Object, source *yaml.RNode) error {
// Convert the given tree into a string. This also handles the source == nil case.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the String function is set on the pointer to RNode and handles itself being nil?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes


// GetCommentSource retrieves the YAML tree used as the source for transferring comments for the given runtime.Object.
// This may be used externally to implement e.g. re-parenting of the comment source tree when moving structs around.
func GetCommentSource(obj runtime.Object) (*yaml.RNode, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please create some simple unit tests for these functions, and use them in the rest of the code internally

Signed-off-by: Dennis Marttinen <dennis@weave.works>
@twelho twelho force-pushed the comment-source-access branch from 0be300f to c69d0f6 Compare July 24, 2020 15:35
twelho added 2 commits July 27, 2020 13:23
Signed-off-by: Dennis Marttinen <dennis@weave.works>
Signed-off-by: Dennis Marttinen <dennis@weave.works>
@twelho twelho force-pushed the comment-source-access branch from ec903ee to ddf485a Compare July 27, 2020 10:33
Signed-off-by: Dennis Marttinen <dennis@weave.works>
Copy link
Contributor

@luxas luxas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, just a few more minor comments.

}

// SetCommentSource sets the given bytes as the source for transferring comments for the given runtime.Object.
func setCommentSourceBytes(obj runtime.Object, source []byte) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as this expects to return either errNoObjectMeta or nil, please use just an ok bool return value so the panic above is unnecessary

if !ok {
// no need to delete the annotation as we know it doesn't exist, just do a normal encode
priorNode, err := getCommentSourceMeta(metaObj)
if err == ErrNoStoredComments {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use errors.Is here instead, to allow future wrapping of the error

if !ok {
// If the object doesn't have ObjectMeta embedded, just do nothing
// Preserve the original file content in the annotation (this requires embedding ObjectMeta).
if err := setCommentSourceBytes(obj, doc); err == ErrNoObjectMeta {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should use just an if !ok { do things } to avoid panicing

@@ -14,6 +15,11 @@ import (

const preserveCommentsAnnotation = "serializer.libgitops.weave.works/original-data"

var (
ErrNoObjectMeta = errors.New("the given object cannot store comments, it is not metav1.ObjectMeta compliant")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a TODO here for the future to investigate if we can make this just require metav1.Object (the interface) instead of ObjectMeta the struct.

Also please add comments to both of these errors

t.Errorf("expected error %t, but received %t: %v", tc.expectedErr, actualErr != nil, actualErr)
}

if actualErr == nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer if actualErr != nil { return // short circuit } pretty much everywhere

Signed-off-by: Dennis Marttinen <dennis@weave.works>
@twelho twelho force-pushed the comment-source-access branch from 645e711 to 1a25766 Compare July 27, 2020 11:48
@twelho
Copy link
Contributor Author

twelho commented Jul 27, 2020

Comments addressed and CI is green, merging...

@twelho twelho merged commit 0a3b512 into weaveworks:master Jul 27, 2020
@twelho twelho deleted the comment-source-access branch July 27, 2020 12:34
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants