aboutsummaryrefslogtreecommitdiff
path: root/docs/varlink/apidoc.go
blob: 9cb190c33926ddc30c54cfd58404072c5bd4fdbf (plain)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
	"sort"
	"strings"

	"github.com/varlink/go/varlink/idl"
)

func readFileToString(path string) (string, error) {
	content, err := ioutil.ReadFile(path)
	if err != nil {
		return "", err
	}
	return string(content), nil
}

func exit(err error) {
	fmt.Println(err.Error())
	os.Exit(1)
}

func typeToString(input *idl.Type) string {
	switch input.Kind {
	case idl.TypeString:
		return "string"
	case idl.TypeBool:
		return "bool"
	case idl.TypeFloat:
		return "float"
	case idl.TypeArray:
		result := input.ElementType.Alias
		if result == "" {
			return fmt.Sprintf("[]%s", typeToString(input.ElementType))
		}
		return result
	case idl.TypeAlias:
		return input.Alias
	case idl.TypeMap:
		return "map[string]"
	case idl.TypeInt:
		return "int"
	}
	return ""
}

func typeToLink(input string) string {
	switch input {
	case "string":
		return "https://godoc.org/builtin#string"
	case "int":
		return "https://godoc.org/builtin#int"
	case "bool":
		return "https://godoc.org/builtin#bool"
	case "float":
		return "https://golang.org/src/builtin/builtin.go#L58"
	default:
		return fmt.Sprintf("#%s", input)
	}
}

type funcArgs struct {
	paramName string
	paramKind string
}
type funcDescriber struct {
	Name         string
	inputParams  []funcArgs
	returnParams []funcArgs
	doc          string
}

type funcSorter []funcDescriber

func (a funcSorter) Len() int           { return len(a) }
func (a funcSorter) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a funcSorter) Less(i, j int) bool { return a[i].Name < a[j].Name }

type typeAttrs struct {
	Name     string
	AttrType string
}
type typeDescriber struct {
	Name  string
	doc   string
	Attrs []typeAttrs
}

type typeSorter []typeDescriber

func (a typeSorter) Len() int           { return len(a) }
func (a typeSorter) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a typeSorter) Less(i, j int) bool { return a[i].Name < a[j].Name }

type err struct {
	Name string
	doc  string
}

type errorSorter []err

func (a errorSorter) Len() int           { return len(a) }
func (a errorSorter) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a errorSorter) Less(i, j int) bool { return a[i].Name < a[j].Name }

// collects defined types in the idl
func getTypes(tidl *idl.IDL) []typeDescriber {
	var types []typeDescriber
	for _, x := range tidl.Aliases {
		i := typeDescriber{
			Name: x.Name,
			doc:  x.Doc,
		}
		ta := []typeAttrs{}
		for _, y := range x.Type.Fields {
			result := typeToString(y.Type)
			ta = append(ta, typeAttrs{Name: y.Name, AttrType: result})
		}
		i.Attrs = ta
		types = append(types, i)
	}
	return types
}

// collects defined methods in the idl
func getMethods(midl *idl.IDL) []funcDescriber {
	var methods []funcDescriber
	for _, t := range midl.Methods {
		m := funcDescriber{
			Name: t.Name,
			doc:  t.Doc,
		}
		fa := []funcArgs{}
		fo := []funcArgs{}

		for _, i := range t.In.Fields {
			fa = append(fa, funcArgs{paramName: i.Name, paramKind: typeToString(i.Type)})

		}
		for _, f := range t.Out.Fields {
			fo = append(fo, funcArgs{paramName: f.Name, paramKind: typeToString(f.Type)})
		}
		m.inputParams = fa
		m.returnParams = fo
		methods = append(methods, m)
	}
	return methods
}

// collects defined errors in the idl
func getErrors(midl *idl.IDL) []err {
	var errors []err
	for _, e := range midl.Errors {
		myError := err{
			Name: e.Name,
			doc:  e.Doc,
		}
		errors = append(errors, myError)
	}
	return errors
}

// generates the index for the top of the markdown page
func generateIndex(methods []funcDescriber, types []typeDescriber, errors []err, b bytes.Buffer) bytes.Buffer {
	// Sort the methods, types, and errors by alphabetical order
	sort.Sort(funcSorter(methods))
	sort.Sort(typeSorter(types))
	sort.Sort(errorSorter(errors))

	for _, method := range methods {
		var inArgs []string
		var outArgs []string
		for _, inArg := range method.inputParams {
			inArgs = append(inArgs, fmt.Sprintf("%s: %s", inArg.paramName, inArg.paramKind))

		}
		for _, outArg := range method.returnParams {
			outArgs = append(outArgs, fmt.Sprintf("%s", outArg.paramKind))

		}
		b.WriteString(fmt.Sprintf("\n[func %s(%s) %s](#%s)\n", method.Name, strings.Join(inArgs, ", "), strings.Join(outArgs, ", "), method.Name))
	}
	for _, t := range types {
		b.WriteString(fmt.Sprintf("[type %s](#%s)\n\n", t.Name, t.Name))
	}
	for _, e := range errors {
		b.WriteString(fmt.Sprintf("[error %s](#%s)\n\n", e.Name, e.Name))
	}
	return b
}

// performs the output for defined methods
func generateFuncDescriptions(methods []funcDescriber, b bytes.Buffer) bytes.Buffer {
	for _, method := range methods {
		b.WriteString(fmt.Sprintf("### <a name=\"%s\"></a>func %s\n", method.Name, method.Name))
		var inArgs []string
		var outArgs []string
		for _, inArg := range method.inputParams {
			inArgs = append(inArgs, fmt.Sprintf("%s: [%s](%s)", inArg.paramName, inArg.paramKind, typeToLink(inArg.paramKind)))
		}
		for _, outArg := range method.returnParams {
			outArgs = append(outArgs, fmt.Sprintf("[%s](%s)", outArg.paramKind, typeToLink(outArg.paramKind)))
		}
		b.WriteString(fmt.Sprintf("<div style=\"background-color: #E8E8E8; padding: 15px; margin: 10px; border-radius: 10px;\">\n\nmethod %s(%s) %s</div>", method.Name, strings.Join(inArgs, ", "), strings.Join(outArgs, ", ")))
		b.WriteString("\n")
		b.WriteString(method.doc)
		b.WriteString("\n")
	}
	return b
}

// performs the output for defined types/structs
func generateTypeDescriptions(types []typeDescriber, b bytes.Buffer) bytes.Buffer {
	for _, t := range types {
		b.WriteString(fmt.Sprintf("### <a name=\"%s\"></a>type %s\n", t.Name, t.Name))
		b.WriteString(fmt.Sprintf("\n%s\n", t.doc))
		for _, i := range t.Attrs {
			b.WriteString(fmt.Sprintf("\n%s [%s](%s)\n", i.Name, i.AttrType, typeToLink(i.AttrType)))
		}
	}
	return b
}

// performs the output for defined errors
func generateErrorDescriptions(errors []err, b bytes.Buffer) bytes.Buffer {
	for _, e := range errors {
		b.WriteString(fmt.Sprintf("### <a name=\"%s\"></a>type %s\n", e.Name, e.Name))
		b.WriteString(fmt.Sprintf("\n%s\n", e.doc))
	}
	return b
}

func main() {
	args := os.Args
	if len(args) < 2 {
		exit(fmt.Errorf("you must provide an input and output path"))
	}
	varlinkFile := args[1]
	mdFile := args[2]

	varlinkInput, err := readFileToString(varlinkFile)
	if err != nil {
		exit(err)
	}
	varlinkInput = strings.TrimRight(varlinkInput, "\n")

	// Run the idl parser
	midl, err := idl.New(varlinkInput)
	if err != nil {
		exit(err)
	}
	// Collect up the info from the idl
	methods := getMethods(midl)
	types := getTypes(midl)
	errors := getErrors(midl)

	out := bytes.Buffer{}
	out.WriteString(fmt.Sprintf("# %s\n", midl.Name))
	out.WriteString(fmt.Sprintf("%s\n", midl.Doc))
	out.WriteString("## Index\n")
	out = generateIndex(methods, types, errors, out)
	out.WriteString("## Methods\n")
	out = generateFuncDescriptions(methods, out)
	out.WriteString("## Types\n")
	out = generateTypeDescriptions(types, out)
	out.WriteString("## Errors\n")
	out = generateErrorDescriptions(errors, out)
	ioutil.WriteFile(mdFile, out.Bytes(), 0755)
}