From 373ee157c95bdaa9d230516b1eb78361a37a2d62 Mon Sep 17 00:00:00 2001 From: Zach Leslie Date: Wed, 9 Sep 2020 16:11:52 -0700 Subject: [PATCH] chore(entities): filter out read-only tag values 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. --- pkg/entities/tags.go | 50 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/pkg/entities/tags.go b/pkg/entities/tags.go index b029d36a..6cf4c189 100644 --- a/pkg/entities/tags.go +++ b/pkg/entities/tags.go @@ -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. @@ -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 + @@ -129,7 +174,8 @@ var listTagsQuery = ` type listTagsResponse struct { Actor struct { Entity struct { - Tags []*Tag + Tags []*Tag + TagsWithMetadata []*EntityTagWithMetadata } } }