Repository navigation
Expand file tree
/
Copy pathapi_reference_test.go
More file actions
116 lines (107 loc) · 3.03 KB
/
Copy pathapi_reference_test.go
File metadata and controls
116 lines (107 loc) · 3.03 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package kvm
import (
"flag"
"fmt"
"os"
"reflect"
"sort"
"strings"
"testing"
)
var updateAPIReference = flag.Bool("update-api-reference", false, "rewrite the method tables in API.md")
const (
apiReferenceStart = "<!-- methods:start -->\n"
apiReferenceEnd = "<!-- methods:end -->\n"
)
func apiReferenceType(t reflect.Type) string {
switch t.Kind() {
case reflect.Bool:
return "boolean"
case reflect.String:
return "string"
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64:
return "number"
case reflect.Slice, reflect.Array:
return apiReferenceType(t.Elem()) + "[]"
case reflect.Pointer:
return apiReferenceType(t.Elem())
case reflect.Struct, reflect.Map:
return "object"
}
return "any"
}
func apiReferenceRow(name string, handler RPCHandler) string {
fn := reflect.TypeOf(handler.Func)
offset := 0
for _, caller := range []reflect.Type{reflect.TypeOf((*Session)(nil)), reflect.TypeOf((*APIToken)(nil))} {
if fn.NumIn() > offset && fn.In(offset) == caller {
offset++
}
}
var params []string
for i, param := range append(append([]string(nil), handler.Params...), handler.OptionalParams...) {
optional := ""
if i >= len(handler.Params) {
optional = "?"
}
params = append(params, fmt.Sprintf("`%s%s`: %s", param, optional, apiReferenceType(fn.In(i+offset))))
}
result := ""
errorType := reflect.TypeOf((*error)(nil)).Elem()
if fn.NumOut() > 0 && !fn.Out(0).Implements(errorType) {
result = apiReferenceType(fn.Out(0))
}
return fmt.Sprintf("| `%s` | %s | %s |\n", name, strings.Join(params, ", "), result)
}
func apiReference() string {
names := make([]string, 0, len(rpcHandlers))
for name := range rpcHandlers {
names = append(names, name)
}
sort.Strings(names)
var out strings.Builder
for _, section := range []struct {
role APIRole
title string
}{
{APIRoleView, "View"},
{APIRoleControl, "Control"},
{APIRoleAdmin, "Admin"},
{APIRoleWebUI, "Web UI only"},
} {
fmt.Fprintf(&out, "### %s\n\n| Method | Parameters | Result |\n|---|---|---|\n", section.title)
for _, name := range names {
if rpcHandlers[name].role() == section.role {
out.WriteString(apiReferenceRow(name, rpcHandlers[name]))
}
}
out.WriteString("\n")
}
return out.String()
}
func TestAPIReferenceIsCurrent(t *testing.T) {
const path = "API.md"
raw, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
doc := string(raw)
start := strings.Index(doc, apiReferenceStart)
end := strings.Index(doc, apiReferenceEnd)
if start < 0 || end < start {
t.Fatalf("%s has no method table markers", path)
}
start += len(apiReferenceStart)
want := apiReference()
if doc[start:end] == want {
return
}
if !*updateAPIReference {
t.Fatalf("the method tables in %s do not match rpcHandlers. Run: go test -run TestAPIReferenceIsCurrent -update-api-reference .", path)
}
if err := os.WriteFile(path, []byte(doc[:start]+want+doc[end:]), 0o644); err != nil {
t.Fatal(err)
}
}