Skip to content

Commit

Permalink
fix: handle contents of tags properly by unquoting them when necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianLoch authored and alecthomas committed Jan 25, 2023
1 parent 95a465b commit 37e8014
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 10 deletions.
3 changes: 2 additions & 1 deletion kong_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
"testing"

"github.com/alecthomas/assert/v2"
"github.com/alecthomas/kong"
"github.com/alecthomas/repr"

"github.com/alecthomas/kong"
)

func mustNew(t *testing.T, cli interface{}, options ...kong.Option) *kong.Kong {
Expand Down
41 changes: 34 additions & 7 deletions tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,39 @@ func (t *Tag) String() string {

type tagChars struct {
sep, quote, assign rune
needsUnquote bool
}

var kongChars = tagChars{sep: ',', quote: '\'', assign: '='}
var bareChars = tagChars{sep: ' ', quote: '"', assign: ':'}
var kongChars = tagChars{sep: ',', quote: '\'', assign: '=', needsUnquote: false}
var bareChars = tagChars{sep: ' ', quote: '"', assign: ':', needsUnquote: true}

// nolint:gocyclo
func parseTagItems(tagString string, chr tagChars) (map[string][]string, error) {
d := map[string][]string{}
key := []rune{}
value := []rune{}
quotes := false
inKey := true

add := func() {
d[string(key)] = append(d[string(key)], string(value))
add := func() error {
// Bare tags are quoted, therefore we need to unquote them in the same fashion reflect.Lookup() (implicitly)
// unquotes "kong tags".
s := string(value)

if chr.needsUnquote && s != "" {
if unquoted, err := strconv.Unquote(fmt.Sprintf(`"%s"`, s)); err == nil {
s = unquoted
} else {
return fmt.Errorf("unquoting tag value `%s`: %w", s, err)
}
}

d[string(key)] = append(d[string(key)], s)
key = []rune{}
value = []rune{}
inKey = true

return nil
}

runes := []rune(tagString)
Expand All @@ -86,7 +102,10 @@ func parseTagItems(tagString string, chr tagChars) (map[string][]string, error)
eof = true
}
if !quotes && r == chr.sep {
add()
if err := add(); err != nil {
return nil, err
}

continue
}
if r == chr.assign && inKey {
Expand All @@ -96,6 +115,12 @@ func parseTagItems(tagString string, chr tagChars) (map[string][]string, error)
if r == '\\' {
if next == chr.quote {
idx++

// We need to keep the backslashes, otherwise subsequent unquoting cannot work
if chr.needsUnquote {
value = append(value, r)
}

r = chr.quote
}
} else if r == chr.quote {
Expand All @@ -119,7 +144,9 @@ func parseTagItems(tagString string, chr tagChars) (map[string][]string, error)
return nil, fmt.Errorf("%v is not quoted properly", tagString)
}

add()
if err := add(); err != nil {
return nil, err
}

return d, nil
}
Expand Down Expand Up @@ -242,7 +269,7 @@ func hydrateTag(t *Tag, typ reflect.Type) error { // nolint: gocyclo
}
t.PlaceHolder = t.Get("placeholder")
t.Enum = t.Get("enum")
scalarType := (typ == nil || !(typ.Kind() == reflect.Slice || typ.Kind() == reflect.Map || typ.Kind() == reflect.Ptr))
scalarType := typ == nil || !(typ.Kind() == reflect.Slice || typ.Kind() == reflect.Map || typ.Kind() == reflect.Ptr)
if t.Enum != "" && !(t.Required || t.HasDefault) && scalarType {
return fmt.Errorf("enum value is only valid if it is either required or has a valid default value")
}
Expand Down
17 changes: 15 additions & 2 deletions tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ import (
"testing"

"github.com/alecthomas/assert/v2"

"github.com/alecthomas/kong"
)

func TestDefaultValueForOptionalArg(t *testing.T) {
var cli struct {
Arg string `kong:"arg,optional,default='πŸ‘Œ'"`
Arg string `kong:"arg,optional,default='\"\\'πŸ‘Œ\\'\"'"`
}
p := mustNew(t, &cli)
_, err := p.Parse(nil)
assert.NoError(t, err)
assert.Equal(t, "πŸ‘Œ", cli.Arg)
assert.Equal(t, "\"'πŸ‘Œ'\"", cli.Arg)
}

func TestNoValueInTag(t *testing.T) {
Expand Down Expand Up @@ -66,6 +67,18 @@ func TestEscapedQuote(t *testing.T) {
assert.Equal(t, "i don't know", cli.DoYouKnow)
}

func TestEscapingInQuotedTags(t *testing.T) {
var cli struct {
Regex1 string `kong:"default='\\d+\n'"`
Regex2 string `default:"\\d+\n"`
}
p := mustNew(t, &cli)
_, err := p.Parse(nil)
assert.NoError(t, err)
assert.Equal(t, "\\d+\n", cli.Regex1)
assert.Equal(t, "\\d+\n", cli.Regex2)
}

func TestBareTags(t *testing.T) {
var cli struct {
Cmd struct {
Expand Down

0 comments on commit 37e8014

Please sign in to comment.