github.com/microsoft/moc@v0.17.1/pkg/tags/tags.go (about) 1 // Copyright (c) Microsoft Corporation 2 // Licensed under the Apache v2.0 license. 3 package tags 4 5 import ( 6 "github.com/microsoft/moc/pkg/errors" 7 common "github.com/microsoft/moc/rpc/common" 8 ) 9 10 // InitTag 11 func InitTag(key, value string) *common.Tag { 12 return &common.Tag{ 13 Key: key, 14 Value: value, 15 } 16 } 17 18 // AddTag 19 func AddTag(key, value string, tags *common.Tags) { 20 tags.Tags = append(tags.GetTags(), InitTag(key, value)) 21 } 22 23 // DeleteTag 24 func DeleteTag(key string, tags *common.Tags) { 25 index := -1 26 tagsList := tags.GetTags() 27 for idx, tag := range tagsList { 28 if tag.GetKey() == key { 29 index = idx 30 break 31 } 32 } 33 34 if index != -1 { 35 tags.Tags = append(tagsList[:index], tagsList[index+1:]...) 36 } 37 return 38 } 39 40 // GetTagValue 41 func GetTagValue(key string, tags *common.Tags) (string, error) { 42 for _, tag := range tags.GetTags() { 43 if tag.GetKey() == key { 44 return tag.GetValue(), nil 45 } 46 } 47 return "", errors.Wrapf(errors.NotFound, "Missing tag %s", key) 48 } 49 50 // AddTagValue 51 func AddTagValue(key, value string, tags *common.Tags) { 52 for _, tag := range tags.GetTags() { 53 if tag.GetKey() == key { 54 tag.Value = value 55 return 56 } 57 } 58 tags.Tags = append(tags.GetTags(), InitTag(key, value)) 59 return 60 } 61 62 // ProtoToMap 63 func ProtoToMap(prototags *common.Tags) map[string]*string { 64 tags := make(map[string]*string, len(prototags.GetTags())) 65 for _, prototag := range prototags.GetTags() { 66 tags[prototag.Key] = &prototag.Value 67 } 68 return tags 69 } 70 71 // MapToProto 72 func MapToProto(tags map[string]*string) *common.Tags { 73 prototags := common.Tags{} 74 for key, value := range tags { 75 tag := common.Tag{ 76 Key: key, 77 Value: *value, 78 } 79 prototags.Tags = append(prototags.GetTags(), &tag) 80 } 81 return &prototags 82 }