-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathgolang.go
More file actions
171 lines (149 loc) · 3.57 KB
/
Copy pathgolang.go
File metadata and controls
171 lines (149 loc) · 3.57 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
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
package golang
import (
_ "embed"
"encoding/json"
"fmt"
"go/format"
"maps"
"slices"
"strings"
"text/template"
"github.com/open-feature/cli/internal/flagset"
"github.com/open-feature/cli/internal/generators"
"golang.org/x/tools/imports"
)
type GolangGenerator struct {
generators.CommonGenerator
}
type Params struct {
GoPackage string
CLIVersion string
}
//go:embed golang.tmpl
var golangTmpl string
func openFeatureType(t flagset.FlagType) string {
switch t {
case flagset.IntType:
return "Int"
case flagset.FloatType:
return "Float"
case flagset.BoolType:
return "Boolean"
case flagset.StringType:
return "String"
case flagset.ObjectType:
return "Object"
default:
return ""
}
}
func typeString(flagType flagset.FlagType) string {
switch flagType {
case flagset.StringType:
return "string"
case flagset.IntType:
return "int64"
case flagset.BoolType:
return "bool"
case flagset.FloatType:
return "float64"
case flagset.ObjectType:
return "map[string]any"
default:
return ""
}
}
func supportImports(flags []flagset.Flag) []string {
var res []string
if len(flags) > 0 {
res = append(res, "\"context\"")
res = append(res, "\"fmt\"")
res = append(res, "\"github.com/open-feature/go-sdk/openfeature\"")
}
slices.Sort(res)
return res
}
func toMapLiteral(value any) string {
assertedMap, ok := value.(map[string]any)
if !ok {
return "nil"
}
// To have a determined order of the object for comparison
keys := slices.Sorted(maps.Keys(assertedMap))
var builder strings.Builder
builder.WriteString("map[string]any{")
for index, key := range keys {
if index > 0 {
builder.WriteString(", ")
}
val := assertedMap[key]
fmt.Fprintf(&builder, `%q: %s`, key, formatNestedValue(val))
}
builder.WriteString("}")
return builder.String()
}
func formatNestedValue(value any) string {
switch val := value.(type) {
case string:
return fmt.Sprintf("%q", val)
case bool:
return fmt.Sprintf("%t", val)
case int, int64, float64:
return fmt.Sprintf("%v", val)
case map[string]any:
return toMapLiteral(val)
case []any:
var sliceBuilder strings.Builder
sliceBuilder.WriteString("[]any{")
for index, elem := range val {
if index > 0 {
sliceBuilder.WriteString(", ")
}
sliceBuilder.WriteString(formatNestedValue(elem))
}
sliceBuilder.WriteString("}")
return sliceBuilder.String()
default:
jsonBytes, err := json.Marshal(val)
if err != nil {
return "nil"
}
return fmt.Sprintf("%q", string(jsonBytes))
}
}
func (g *GolangGenerator) Generate(params *generators.Params[Params]) error {
funcs := template.FuncMap{
"SupportImports": supportImports,
"OpenFeatureType": openFeatureType,
"TypeString": typeString,
"ToMapLiteral": toMapLiteral,
}
newParams := &generators.Params[any]{
OutputPath: params.OutputPath,
TemplatePath: params.TemplatePath,
Custom: Params{
GoPackage: params.Custom.GoPackage,
CLIVersion: params.Custom.CLIVersion,
},
}
filename := params.Custom.GoPackage + "_gen.go"
return g.GenerateFile(funcs, golangTmpl, newParams, filename)
}
// NewGenerator creates a generator for Go.
func NewGenerator(fs *flagset.Flagset) *GolangGenerator {
g := &GolangGenerator{
CommonGenerator: *generators.NewGenerator(fs, map[flagset.FlagType]bool{}),
}
g.Formatter = func(data []byte) ([]byte, error) {
data, err := format.Source(data)
if err != nil {
return nil, fmt.Errorf("failed to format go code: %w", err)
}
data, err = imports.Process("", data, nil)
if err != nil {
return nil, fmt.Errorf("failed to format imports: %w", err)
}
return data, nil
}
return g
}