Skip to content

Commit

Permalink
Changes from review
Browse files Browse the repository at this point in the history
  • Loading branch information
lverma14 committed Mar 9, 2023
1 parent 0ad3a6c commit f541237
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions annotated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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] != '"' {
Expand All @@ -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
}
Expand Down

0 comments on commit f541237

Please sign in to comment.