Skip to content

Commit a8e132d

Browse files
committed
Fix issue with validation
1 parent 011de6b commit a8e132d

4 files changed

Lines changed: 19 additions & 13 deletions

File tree

opts.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ox
22

33
import (
4+
"cmp"
45
"context"
56
"fmt"
67
"io"
@@ -551,7 +552,7 @@ func Special(special string) FlagOption {
551552
}
552553

553554
// Valid is a [Flag] option to add a allowed value validator to a flag.
554-
func Valid[T comparable](values ...T) FlagOption {
555+
func Valid[T cmp.Ordered](values ...T) FlagOption {
555556
return option{
556557
name: "Valid",
557558
flag: func(g *Flag) error {

ox.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -441,13 +441,14 @@ func (ctx *Context) Populate(cmd *Command, all, overwrite bool) error {
441441
case g.Type == HookT, g.Def == nil && !all:
442442
continue
443443
case g.Def != nil:
444-
var err error
445-
if value, err = ctx.Expand(g.Def); err != nil {
446-
return err
444+
v, err := ctx.Expand(g.Def)
445+
if err != nil {
446+
return fmt.Errorf("populate %s: cannot expand %v (%T): %w", g.Name, g.Def, g.Def, err)
447447
}
448+
value, _ = asString[string](v)
448449
}
449450
if err := ctx.Vars.Set(ctx, g, value, false); err != nil {
450-
return fmt.Errorf("cannot populate %s with %q: %w", g.Name, value, err)
451+
return fmt.Errorf("populate %s: %w", g.Name, err)
451452
}
452453
}
453454
return nil
@@ -469,10 +470,10 @@ func (ctx *Context) Populate(cmd *Command, all, overwrite bool) error {
469470
// $CONFIG_TYPE{KEY} - the registered config file loader type and key value, for example: `$YAML{my_key}`, `$TOML{my_key}`
470471
//
471472
// TODO: finish implementation for $CONFIG_TYPE, expand anywhere in string
472-
func (ctx *Context) Expand(v any) (string, error) {
473+
func (ctx *Context) Expand(v any) (any, error) {
473474
s, ok := v.(string)
474475
if !ok {
475-
return asString[string](v)
476+
return v, nil
476477
}
477478
if ctx.Override != nil {
478479
if s, ok := ctx.Override[s]; ok {
@@ -524,7 +525,7 @@ func (ctx *Context) Expand(v any) (string, error) {
524525
}
525526
s, err := f()
526527
if err != nil {
527-
return "", fmt.Errorf("unable to expand %q: %w", s, err)
528+
return "", fmt.Errorf("expand %q: %w", v, err)
528529
}
529530
return s, nil
530531
}

type.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -319,11 +319,15 @@ func typeRef(val any) Type {
319319
case *url.URL:
320320
return URLT
321321
}
322-
s := reflect.TypeOf(val).String()
323-
if typ, ok := reflectTypes[s]; ok {
324-
return typ
322+
typ := reflect.TypeOf(val)
323+
if typ != nil {
324+
s := typ.String()
325+
if typ, ok := reflectTypes[s]; ok {
326+
return typ
327+
}
328+
return Type(s)
325329
}
326-
return Type(s)
330+
return ""
327331
}
328332

329333
// defaultType returns the type, map key type, and element type of v.

value.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ func invalid(val any) bool {
709709
}
710710

711711
// valid creates a validator func for the provided values.
712-
func valid[T comparable](values ...T) func(any) error {
712+
func valid[T cmp.Ordered](values ...T) func(any) error {
713713
return func(val any) error {
714714
switch v, err := as[T](val, layout(val)); {
715715
case err != nil:

0 commit comments

Comments
 (0)