diff --git a/macro.go b/macro.go index f307c25..8d104ba 100644 --- a/macro.go +++ b/macro.go @@ -61,26 +61,7 @@ func MacroI[T any](f func(t T) carapace.Action) Macro { } return f(t) }, - Signature: func() string { - var t interface{} = new(T) - //if elem := reflect.TypeOf(t).Elem(); elem.Kind() == reflect.Slice { - // TODO slice member - //println(reflect.TypeOf(elem.Elem()).Kind().String()) - //t = reflect.New(reflect.TypeOf(elem.Elem())) - //} - - out, err := yaml.Marshal(t) - if err != nil { - return err.Error() - } - lines := strings.Split(string(out), "\n") - - if reflect.ValueOf(t).Elem().Kind() == reflect.Struct { - return fmt.Sprintf("{%v}", strings.Join(lines[:len(lines)-1], ", ")) - } else { - return fmt.Sprintf("%v", strings.Join(lines[:len(lines)-1], ", ")) - } - }, + Signature: func() string { return signature(new(T)) }, } } @@ -97,6 +78,29 @@ func MacroVarI[T any](f func(s ...T) carapace.Action) Macro { } return f(t...) }, - Signature: func() string { return "TODO" }, + Signature: func() string { return fmt.Sprintf("[%v]", signature(new(T))) }, + } +} + +func signature(i interface{}) string { + elem := reflect.ValueOf(i).Elem() + switch elem.Kind() { + case reflect.Struct: + out, err := yaml.Marshal(i) + if err != nil { + return err.Error() + } + lines := strings.Split(string(out), "\n") + return fmt.Sprintf("{%v}", strings.Join(lines[:len(lines)-1], ", ")) + + case reflect.Slice: + ptr := reflect.New(elem.Type().Elem()).Interface() + return fmt.Sprintf("[%v]", signature(ptr)) + + case reflect.String: + return `""` + + default: + return fmt.Sprintf("%v", reflect.ValueOf(i).Elem()) } } diff --git a/macro_test.go b/macro_test.go index 4b74b8c..a5885c5 100644 --- a/macro_test.go +++ b/macro_test.go @@ -12,13 +12,33 @@ type Arg struct { } func TestSignature(t *testing.T) { - // TODO verify signature := MacroI(func(a Arg) carapace.Action { return carapace.ActionValues() }).Signature() - println(signature) + if expected := `{name: "", option: false}`; signature != expected { + t.Errorf("should be: %v", expected) + } signature = MacroI(func(a []Arg) carapace.Action { return carapace.ActionValues() }).Signature() - println(signature) + if expected := `[{name: "", option: false}]`; signature != expected { + t.Errorf("should be: %v", expected) + } signature = MacroI(func(b bool) carapace.Action { return carapace.ActionValues() }).Signature() - println(signature) + if expected := `false`; signature != expected { + t.Errorf("should be: %v", expected) + } + + signature = MacroVarI(func(a ...Arg) carapace.Action { return carapace.ActionValues() }).Signature() + if expected := `[{name: "", option: false}]`; signature != expected { + t.Errorf("should be: %v", expected) + } + + signature = MacroVarI(func(b ...bool) carapace.Action { return carapace.ActionValues() }).Signature() + if expected := `[false]`; signature != expected { + t.Errorf("should be: %v", expected) + } + + signature = MacroI(func(s string) carapace.Action { return carapace.ActionValues() }).Signature() + if expected := `""`; signature != expected { + t.Errorf("should be: %v", expected) + } }