Skip to content

Commit

Permalink
fix(entities): filter out read-only tag values
Browse files Browse the repository at this point in the history
Without this change, all tags are included in the list operation which leads to
user surprise when modifications are attempted.  Here we modify the method
response to exclude those tags which are not modifiable by the user.
  • Loading branch information
zlesnr committed Sep 10, 2020
1 parent 94a0504 commit 2b496d4
Showing 1 changed file with 48 additions and 2 deletions.
50 changes: 48 additions & 2 deletions pkg/entities/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,36 @@ func (e *Entities) ListTags(guid string) ([]*Tag, error) {
return nil, err
}

return resp.Actor.Entity.Tags, nil
return filterMutable(resp)
}

// filterMutable removes tag values that are read-only from the received response.
func filterMutable(resp listTagsResponse) ([]*Tag, error) {
var tags []*Tag

for _, responseTag := range resp.Actor.Entity.TagsWithMetadata {
if responseTag != nil {
tag := Tag{
Key: responseTag.Key,
}

mutable := 0
for _, responseTagValue := range responseTag.Values {
if responseTagValue.Mutable {
mutable++
tag.Values = append(tag.Values, responseTagValue.Value)
}
}

// All values were mutable
if len(responseTag.Values) == mutable {
tags = append(tags, &tag)
}

}
}

return tags, nil
}

// AddTags writes tags to the entity specified by the provided entity GUID.
Expand Down Expand Up @@ -121,6 +150,22 @@ func parseTagMutationErrors(errors []tagMutationError) string {
return strings.Join(messages, ", ")
}

// EntityTagValueWithMetadata - The value and metadata of a single entity tag.
type EntityTagValueWithMetadata struct {
// Whether or not the tag can be mutated by the user.
Mutable bool `json:"mutable"`
// The tag value.
Value string `json:"value"`
}

// EntityTagWithMetadata - The tags with metadata of the entity.
type EntityTagWithMetadata struct {
// The tag's key.
Key string `json:"key"`
// A list of tag values with metadata information.
Values []EntityTagValueWithMetadata `json:"values"`
}

var listTagsQuery = `
query($guid:EntityGuid!) { actor { entity(guid: $guid) {` +
graphqlEntityStructTagsFields +
Expand All @@ -129,7 +174,8 @@ var listTagsQuery = `
type listTagsResponse struct {
Actor struct {
Entity struct {
Tags []*Tag
Tags []*Tag
TagsWithMetadata []*EntityTagWithMetadata
}
}
}
Expand Down

0 comments on commit 2b496d4

Please sign in to comment.