Skip to content

Commit

Permalink
Merge pull request #35 from rsteube/slice-signature
Browse files Browse the repository at this point in the history
fix signature
  • Loading branch information
rsteube authored May 6, 2022
2 parents f4951e1 + 3c519ba commit a08bf7e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 25 deletions.
46 changes: 25 additions & 21 deletions macro.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)) },
}
}

Expand All @@ -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())
}
}
28 changes: 24 additions & 4 deletions macro_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit a08bf7e

Please sign in to comment.