-
Notifications
You must be signed in to change notification settings - Fork 36
/
main.go
570 lines (484 loc) · 11.4 KB
/
main.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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
package main
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"text/template"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
reMsgName = regexp.MustCompile("^[A-Z0-9_]+$")
reTypeIsArray = regexp.MustCompile(`^(.+?)\[([0-9]+)\]$`)
)
var tplDialect = template.Must(template.New("").Parse(
`//autogenerated:yes
{{- if .Comment }}
// {{ .Comment }}
{{- end }}
package {{ .PkgName }}
import (
"github.com/aler9/gomavlib/pkg/msg"
"github.com/aler9/gomavlib/pkg/dialect"
)
// Dialect contains the dialect definition.
var Dialect = dial
// dial is not exposed directly in order not to display it in godoc.
var dial = &dialect.Dialect{
Version: {{.Version}},
Messages: []msg.Message{
{{- range .Defs }}
// {{ .Name }}
{{- range .Messages }}
&Message{{ .Name }}{},
{{- end }}
{{- end }}
},
}
`))
var tplEnum = template.Must(template.New("").Parse(
`//autogenerated:yes
//nolint:revive,misspell,govet,lll,dupl
package {{ .PkgName }}
import (
"errors"
)
{{- range .Enum.Description }}
// {{ . }}
{{- end }}
type {{ .Enum.Name }} int
const (
{{- $pn := .Enum.Name }}
{{- range .Enum.Values }}
{{- range .Description }}
// {{ . }}
{{- end }}
{{ .Name }} {{ $pn }} = {{ .Value }}
{{- end }}
)
var labels_{{ .Enum.Name }} = map[{{ .Enum.Name }}]string{
{{- range .Values }}
{{ .Name }}: "{{ .Name }}",
{{- end }}
}
// MarshalText implements the encoding.TextMarshaler interface.
func (e {{ .Enum.Name }}) MarshalText() ([]byte, error) {
if l, ok := labels_{{ .Enum.Name }}[e]; ok {
return []byte(l), nil
}
return nil, errors.New("invalid value")
}
var reverseLabels_{{ .Enum.Name }} = map[string]{{ .Enum.Name }}{
{{- range .Values }}
"{{ .Name }}": {{ .Name }},
{{- end }}
}
// UnmarshalText implements the encoding.TextUnmarshaler interface.
func (e *{{ .Enum.Name }}) UnmarshalText(text []byte) error {
if rl, ok := reverseLabels_{{ .Enum.Name }}[string(text)]; ok {
*e = rl
return nil
}
return errors.New("invalid value")
}
// String implements the fmt.Stringer interface.
func (e {{ .Enum.Name }}) String() string {
if l, ok := labels_{{ .Enum.Name }}[e]; ok {
return l
}
return "invalid value"
}
`))
var tplMessage = template.Must(template.New("").Parse(
`//autogenerated:yes
//nolint:revive,misspell,govet,lll
{{- if .Comment }}
// {{ .Comment }}
{{- end }}
package {{ .PkgName }}
{{- range .Msg.Description }}
// {{ . }}
{{- end }}
type Message{{ .Msg.Name }} struct {
{{- range .Msg.Fields }}
{{- range .Description }}
// {{ . }}
{{- end }}
{{ .Line }}
{{- end }}
}
// GetID implements the msg.Message interface.
func (*Message{{ .Msg.Name }}) GetID() uint32 {
return {{ .Msg.ID }}
}
`))
var dialectTypeToGo = map[string]string{
"double": "float64",
"uint64_t": "uint64",
"int64_t": "int64",
"float": "float32",
"uint32_t": "uint32",
"int32_t": "int32",
"uint16_t": "uint16",
"int16_t": "int16",
"uint8_t": "uint8",
"int8_t": "int8",
"char": "string",
}
func dialectNameGoToDef(in string) string {
re := regexp.MustCompile("([A-Z])")
in = re.ReplaceAllString(in, "_${1}")
return strings.ToLower(in[1:])
}
func dialectNameDefToGo(in string) string {
re := regexp.MustCompile("_[a-z]")
in = strings.ToLower(in)
in = re.ReplaceAllStringFunc(in, func(match string) string {
return strings.ToUpper(match[1:2])
})
return strings.ToUpper(in[:1]) + in[1:]
}
func parseDescription(in string) []string {
lines := strings.Split(in, "\n")
for i := range lines {
lines[i] = strings.Trim(lines[i], " ")
}
if lines[len(lines)-1] == "" {
lines = lines[:len(lines)-1]
}
return lines
}
type outEnumValue struct {
Value string
Name string
Description []string
}
type outEnum struct {
Name string
Description []string
Values []*outEnumValue
}
type outField struct {
Description []string
Line string
}
type outMessage struct {
OrigName string
Name string
Description []string
ID int
Fields []*outField
}
type outDefinition struct {
Name string
Enums []*outEnum
Messages []*outMessage
}
func processDefinition(
version *string,
defsProcessed map[string]struct{},
isRemote bool,
defAddr string) ([]*outDefinition, error) {
// skip already processed
if _, ok := defsProcessed[defAddr]; ok {
return nil, nil
}
defsProcessed[defAddr] = struct{}{}
fmt.Fprintf(os.Stderr, "definition %s\n", defAddr)
content, err := getDefinition(isRemote, defAddr)
if err != nil {
return nil, err
}
def, err := definitionDecode(content)
if err != nil {
return nil, fmt.Errorf("unable to decode: %s", err)
}
addrPath, addrName := filepath.Split(defAddr)
var outDefs []*outDefinition
// version
if def.Version != "" {
if *version != "" && *version != def.Version {
return nil, fmt.Errorf("version defined twice (%s and %s)", def.Version, *version)
}
*version = def.Version
}
// includes
for _, inc := range def.Includes {
// prepend url to remote address
if isRemote {
inc = addrPath + inc
}
subDefs, err := processDefinition(version, defsProcessed, isRemote, inc)
if err != nil {
return nil, err
}
outDefs = append(outDefs, subDefs...)
}
outDef := &outDefinition{
Name: addrName,
}
// enums
for _, enum := range def.Enums {
oute := &outEnum{
Name: enum.Name,
Description: parseDescription(enum.Description),
}
for _, val := range enum.Values {
oute.Values = append(oute.Values, &outEnumValue{
Value: val.Value,
Name: val.Name,
Description: parseDescription(val.Description),
})
}
outDef.Enums = append(outDef.Enums, oute)
}
// messages
for _, msg := range def.Messages {
outMsg, err := processMessage(msg)
if err != nil {
return nil, err
}
outDef.Messages = append(outDef.Messages, outMsg)
}
outDefs = append(outDefs, outDef)
return outDefs, nil
}
func getDefinition(isRemote bool, defAddr string) ([]byte, error) {
if isRemote {
byt, err := download(defAddr)
if err != nil {
return nil, fmt.Errorf("unable to download: %s", err)
}
return byt, nil
}
byt, err := ioutil.ReadFile(defAddr)
if err != nil {
return nil, fmt.Errorf("unable to open: %s", err)
}
return byt, nil
}
func download(addr string) ([]byte, error) {
res, err := http.Get(addr)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("bad return code: %v", res.StatusCode)
}
byt, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
return byt, nil
}
func processMessage(msg *definitionMessage) (*outMessage, error) {
if m := reMsgName.FindStringSubmatch(msg.Name); m == nil {
return nil, fmt.Errorf("unsupported message name: %s", msg.Name)
}
outMsg := &outMessage{
OrigName: msg.Name,
Name: dialectNameDefToGo(msg.Name),
Description: parseDescription(msg.Description),
ID: msg.ID,
}
for _, f := range msg.Fields {
outField, err := processField(f)
if err != nil {
return nil, err
}
outMsg.Fields = append(outMsg.Fields, outField)
}
return outMsg, nil
}
func processField(field *dialectField) (*outField, error) {
outF := &outField{
Description: parseDescription(field.Description),
}
tags := make(map[string]string)
newname := dialectNameDefToGo(field.Name)
// name conversion is not univoque: add tag
if dialectNameGoToDef(newname) != field.Name {
tags["mavname"] = field.Name
}
outF.Line += newname
typ := field.Type
arrayLen := ""
if typ == "uint8_t_mavlink_version" {
typ = "uint8_t"
}
// string or array
if matches := reTypeIsArray.FindStringSubmatch(typ); matches != nil {
// string
if matches[1] == "char" {
tags["mavlen"] = matches[2]
typ = "char"
// array
} else {
arrayLen = matches[2]
typ = matches[1]
}
}
// extension
if field.Extension {
tags["mavext"] = "true"
}
typ = dialectTypeToGo[typ]
if typ == "" {
return nil, fmt.Errorf("unknown type: %s", typ)
}
outF.Line += " "
if arrayLen != "" {
outF.Line += "[" + arrayLen + "]"
}
if field.Enum != "" {
outF.Line += field.Enum
tags["mavenum"] = typ
} else {
outF.Line += typ
}
if len(tags) > 0 {
var tmp []string
for k, v := range tags {
tmp = append(tmp, fmt.Sprintf("%s:\"%s\"", k, v))
}
sort.Strings(tmp)
outF.Line += " `" + strings.Join(tmp, " ") + "`"
}
return outF, nil
}
func writeDialect(
pkgName string,
comment string,
version string,
outDefs []*outDefinition,
enums map[string]*outEnum,
) error {
var buf bytes.Buffer
err := tplDialect.Execute(&buf, map[string]interface{}{
"PkgName": pkgName,
"Comment": comment,
"Version": func() int {
ret, _ := strconv.Atoi(version)
return ret
}(),
"Defs": outDefs,
"Enums": enums,
})
if err != nil {
return err
}
return ioutil.WriteFile("dialect.go", buf.Bytes(), 0o644)
}
func writeEnum(
pkgName string,
enum *outEnum) error {
var buf bytes.Buffer
err := tplEnum.Execute(&buf, map[string]interface{}{
"PkgName": pkgName,
"Enum": enum,
})
if err != nil {
return err
}
return ioutil.WriteFile("enum_"+strings.ToLower(enum.Name)+".go", buf.Bytes(), 0o644)
}
func writeMessage(
pkgName string,
msg *outMessage) error {
var buf bytes.Buffer
err := tplMessage.Execute(&buf, map[string]interface{}{
"PkgName": pkgName,
"Msg": msg,
})
if err != nil {
return err
}
return ioutil.WriteFile("msg_"+strings.ToLower(msg.OrigName)+".go", buf.Bytes(), 0o644)
}
func run() error {
kingpin.CommandLine.Help = "Convert Mavlink dialects from XML format into Go format."
argPwd := kingpin.Flag("pwd", "Target directory").Default("").String()
argPkgName := kingpin.Flag("package", "Package name").Default("main").String()
argComment := kingpin.Flag("comment", "comment to add before the package name").Default("").String()
argMainDef := kingpin.Arg("xml", "Path or url pointing to a XML Mavlink dialect").Required().String()
kingpin.Parse()
pwd := *argPwd
pkgName := *argPkgName
comment := *argComment
mainDef := *argMainDef
if pwd != "" {
os.Chdir(pwd)
}
version := ""
defsProcessed := make(map[string]struct{})
isRemote := func() bool {
_, err := url.ParseRequestURI(mainDef)
return err == nil
}()
// parse all definitions recursively
outDefs, err := processDefinition(&version, defsProcessed, isRemote, mainDef)
if err != nil {
return err
}
// merge enums together
enums := make(map[string]*outEnum)
for _, def := range outDefs {
for _, defEnum := range def.Enums {
if _, ok := enums[defEnum.Name]; !ok {
enums[defEnum.Name] = &outEnum{
Name: defEnum.Name,
Description: defEnum.Description,
}
}
enum := enums[defEnum.Name]
enum.Values = append(enum.Values, defEnum.Values...)
}
}
// fill enum missing values
for _, enum := range enums {
nextVal := 0
for _, v := range enum.Values {
if v.Value != "" {
nextVal, _ = strconv.Atoi(v.Value)
nextVal++
} else {
v.Value = strconv.Itoa(nextVal)
nextVal++
}
}
}
err = writeDialect(pkgName, comment, version, outDefs, enums)
if err != nil {
return err
}
for _, enum := range enums {
err := writeEnum(pkgName, enum)
if err != nil {
return err
}
}
for _, def := range outDefs {
for _, msg := range def.Messages {
err := writeMessage(pkgName, msg)
if err != nil {
return err
}
}
}
return nil
}
func main() {
err := run()
if err != nil {
fmt.Fprintf(os.Stderr, "ERR: %s\n", err)
os.Exit(1)
}
}