From f5412376c8d15e6b9486db0a85168f00ab0e7b91 Mon Sep 17 00:00:00 2001 From: lverma14 Date: Wed, 8 Mar 2023 19:16:18 -0800 Subject: [PATCH] Changes from review --- annotated.go | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/annotated.go b/annotated.go index 5e508f2d6..a438a27b5 100644 --- a/annotated.go +++ b/annotated.go @@ -151,20 +151,16 @@ var ( errTagValueSyntaxEndingQuote = errors.New(`tag value should end in double quote. i.e. key:"value" `) ) -// Given a tag with key, value pair, they must be separaed by a space. +// Collections of key value pairs within a tag should be separated by a space. // Eg: `group:"some" optional:"true"` -// return an error if in the case of multiple tags, they aren't separated -// space. Return nil on success. -func verifyTagsSpaceSeparated(numTags int, tag string) error { - if numTags > 0 && tag != "" && tag[0] != ' ' { +func verifyTagsSpaceSeparated(tagIdx int, tag string) error { + if tagIdx > 0 && tag != "" && tag[0] != ' ' { return errTagSyntaxSpace } return nil } -// Given a value from a tag verify that it is contained between double quotes -// Return an error if the invalid quote format is used for tag value. -// On sucess return the remaining tag with nil error +// verify tag values are delimited with double quotes func verifyValueQuote(value string) (string, error) { // starting quote should be a double quote if value[0] != '"' { @@ -190,24 +186,21 @@ func verifyValueQuote(value string) (string, error) { // tag:"value" space-separated list ) // Currently dig accepts only 'name', 'group', 'optional' as valid tag keys func verifyAnnotateTag(tag string) error { - numTags := 0 - for ; tag != ""; numTags++ { - validKeys := map[string]bool{"group": true, "optional": true, "name": true} - if err := verifyTagsSpaceSeparated(numTags, tag); err != nil { + tagIdx := 0 + for ; tag != ""; tagIdx++ { + validKeys := map[string]struct{}{"group": {}, "optional": {}, "name": {}} + if err := verifyTagsSpaceSeparated(tagIdx, tag); err != nil { return err } i := 0 - // return if tag is empty if strings.TrimSpace(tag) == "" { return nil } - // parsing the key i.e. till reaching colon : for i < len(tag) && tag[i] != ':' { i++ } key := strings.TrimSpace(tag[:i]) - // if key is not equal to group name or optional if _, ok := validKeys[key]; !ok { return errTagKeySyntax }