This repository has been archived by the owner on Jan 21, 2020. It is now read-only.
/
help.go
83 lines (71 loc) · 1.62 KB
/
help.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package template
import (
"fmt"
"reflect"
"strings"
)
// UpdateDocumentation uses reflection to generate documentation on usage and function signature.
func UpdateDocumentation(in []Function) []Function {
out := []Function{}
for _, f := range in {
copy := f
copy.Function = functionSignature(f.Name, f.Func)
copy.Usage = functionUsage(f.Name, f.Func)
if len(f.Description) == 0 {
copy.Description = []string{"None"}
}
out = append(out, copy)
}
return out
}
func isFunc(f interface{}) (string, bool) {
if f == nil {
return "no-function", false
}
ft := reflect.TypeOf(f)
if ft.Kind() != reflect.Func {
return "not-a-function", false
}
return ft.String(), true
}
func functionSignature(name string, f interface{}) string {
s, is := isFunc(f)
if !is {
return s
}
return s
}
func functionUsage(name string, f interface{}) string {
if s, is := isFunc(f); !is {
return s
}
ft := reflect.TypeOf(f)
if ft.Kind() != reflect.Func {
return "not-a-function"
}
args := make([]string, ft.NumIn())
for i := 0; i < len(args); i++ {
t := ft.In(i)
v := ""
switch {
case t == reflect.TypeOf(""):
v = fmt.Sprintf("\"%s\"", t.Name())
case t.Kind() == reflect.Slice && i == len(args)-1:
tt := t.Elem().Name()
if t.Elem() == reflect.TypeOf("") {
tt = fmt.Sprintf("\"%s\"", t.Name())
}
v = fmt.Sprintf("[ %s ... ]", tt)
case t.String() == "interface {}":
v = "any"
default:
v = strings.Replace(t.String(), " ", "", -1)
}
args[i] = v
}
arglist := strings.Join(args, " ")
if len(arglist) > 0 {
arglist = arglist + " "
}
return fmt.Sprintf("{{ %s %s}}", name, arglist)
}