-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscheme.go
113 lines (97 loc) · 2.47 KB
/
scheme.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
//go:build generator
package main
import (
"fmt"
"go/types"
"io"
"text/template"
)
const schemeTemplate = `@namespace("{{ .Namespace }}") protocol {{ .Protocol }} {
/** {{ .Record }} is the telemetry data for the product. */
@df_datatype("{{ .DataFabricDataType }}") record {{ .Record }} {
/** The field that identifies what type of data this is. */
string dataType;
/** The time the event occurred */
long eventTime;
/** The time our edge ingested the event */
long ingestTime;
{{ range .Fields }}
/** {{ .Comment }} */
{{ .Type }} {{ .Name }} = null;
{{ end }}
}
}
`
func getAvroPrimitiveType(kind types.BasicKind) string {
switch kind {
case types.Int64:
return "long"
case types.Float64:
return "double"
case types.String:
return "string"
case types.Bool:
return "boolean"
default:
panic(fmt.Sprintf("unexpected kind %v", kind))
}
}
type schemeGen struct {
Namespace string
Protocol string
DataFabricDataType string
Record string
Fields []schemeField
}
type schemeField struct {
Comment string
Type string
Name string
}
type schemeGenConfig struct {
namespace string
protocol string
dataFabricDataType string
record string
fields []field
}
func generateScheme(writer io.Writer, cfg schemeGenConfig) error {
var schemeFields []schemeField
var createSchemeFields func([]field)
createSchemeFields = func(fields []field) {
for _, f := range fields {
switch {
case f.slice:
schemeFields = append(schemeFields, schemeField{
Comment: f.docString,
Type: fmt.Sprintf("union {null, array<%s>}", getAvroPrimitiveType(f.fieldType)),
Name: f.name,
})
case f.embeddedStruct:
createSchemeFields(f.embeddedStructFields)
default:
schemeFields = append(schemeFields, schemeField{
Comment: f.docString,
Type: getAvroPrimitiveType(f.fieldType) + "?",
Name: f.name,
})
}
}
}
createSchemeFields(cfg.fields)
sg := schemeGen{
Namespace: cfg.namespace,
Protocol: cfg.protocol,
DataFabricDataType: cfg.dataFabricDataType,
Record: cfg.record,
Fields: schemeFields,
}
funcMap := template.FuncMap{
"getAttributeType": getAttributeType,
}
tmpl := template.Must(template.New("scheme").Funcs(funcMap).Parse(schemeTemplate))
if err := tmpl.Execute(writer, sg); err != nil {
return fmt.Errorf("failed to execute template: %w", err)
}
return nil
}