Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (
)

var (
errTagSyntax = errors.New("bad syntax for struct tag pair")
errTagKeySyntax = errors.New("bad syntax for struct tag key")
errTagValueSyntax = errors.New("bad syntax for struct tag value")
ErrTagSyntax = errors.New("bad syntax for struct tag pair")
ErrTagKeySyntax = errors.New("bad syntax for struct tag key")
ErrTagValueSyntax = errors.New("bad syntax for struct tag value")

errKeyNotSet = errors.New("tag key does not exist")
errTagNotExist = errors.New("tag does not exist")
ErrKeyNotSet = errors.New("tag key does not exist")
ErrTagNotExist = errors.New("tag does not exist")
errTagKeyMismatch = errors.New("mismatch between key and tag.key")
)

Expand Down Expand Up @@ -69,13 +69,13 @@ func Parse(tag string) (*Tags, error) {
}

if i == 0 {
return nil, errTagKeySyntax
return nil, ErrTagKeySyntax
}
if i+1 >= len(tag) || tag[i] != ':' {
return nil, errTagSyntax
return nil, ErrTagSyntax
}
if tag[i+1] != '"' {
return nil, errTagValueSyntax
return nil, ErrTagValueSyntax
}

key := string(tag[:i])
Expand All @@ -90,15 +90,15 @@ func Parse(tag string) (*Tags, error) {
i++
}
if i >= len(tag) {
return nil, errTagValueSyntax
return nil, ErrTagValueSyntax
}

qvalue := string(tag[:i+1])
tag = tag[i+1:]

value, err := strconv.Unquote(qvalue)
if err != nil {
return nil, errTagValueSyntax
return nil, ErrTagValueSyntax
}

res := strings.Split(value, ",")
Expand Down Expand Up @@ -126,22 +126,22 @@ func Parse(tag string) (*Tags, error) {

// Get returns the tag associated with the given key. If the key is present
// in the tag the value (which may be empty) is returned. Otherwise the
// returned value will be the empty string. The ok return value reports whether
// the tag exists or not (which the return value is nil).
// returned value will be the empty string. The error return value reports whether
// the tag exists or not (in which case the return value is nil).
func (t *Tags) Get(key string) (*Tag, error) {
for _, tag := range t.tags {
if tag.Key == key {
return tag, nil
}
}

return nil, errTagNotExist
return nil, ErrTagNotExist
}

// Set sets the given tag. If the tag key already exists it'll override it
func (t *Tags) Set(tag *Tag) error {
if tag.Key == "" {
return errKeyNotSet
return ErrKeyNotSet
}

added := false
Expand Down
2 changes: 1 addition & 1 deletion tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ func TestTags_Set_KeyDoesNotExist(t *testing.T) {
t.Fatal("setting tag with a nonexisting key should error")
}

if err != errKeyNotSet {
if err != ErrKeyNotSet {
t.Errorf("set\n\twant: %#v\n\tgot : %#v", errTagKeyMismatch, err)
}
}
Expand Down