-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathpython.go
More file actions
164 lines (140 loc) · 3.57 KB
/
Copy pathpython.go
File metadata and controls
164 lines (140 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
package python
import (
_ "embed"
"encoding/json"
"fmt"
"maps"
"slices"
"strings"
"text/template"
"github.com/open-feature/cli/internal/flagset"
"github.com/open-feature/cli/internal/generators"
)
type PythonGenerator struct {
generators.CommonGenerator
}
type Params struct{}
//go:embed python.tmpl
var pythonTmpl string
func openFeatureType(t flagset.FlagType) string {
switch t {
case flagset.IntType:
return "int"
case flagset.FloatType:
return "float"
case flagset.BoolType:
return "bool"
case flagset.StringType:
return "str"
default:
return "object"
}
}
func methodType(flagType flagset.FlagType) string {
switch flagType {
case flagset.StringType:
return "string"
case flagset.IntType:
return "integer"
case flagset.BoolType:
return "boolean"
case flagset.FloatType:
return "float"
case flagset.ObjectType:
return "object"
default:
panic("unsupported flag type")
}
}
func typedGetMethodSync(flagType flagset.FlagType) string {
return "get_" + methodType(flagType) + "_value"
}
func typedGetMethodAsync(flagType flagset.FlagType) string {
return "get_" + methodType(flagType) + "_value_async"
}
func typedDetailsMethodSync(flagType flagset.FlagType) string {
return "get_" + methodType(flagType) + "_details"
}
func typedDetailsMethodAsync(flagType flagset.FlagType) string {
return "get_" + methodType(flagType) + "_details_async"
}
func pythonBoolLiteral(value any) any {
if v, ok := value.(bool); ok {
if v {
return "True"
}
return "False"
}
return value
}
func toPythonDict(value any) string {
assertedMap, ok := value.(map[string]any)
if !ok {
return "None"
}
// To have a determined order of the object for comparison
keys := slices.Sorted(maps.Keys(assertedMap))
var builder strings.Builder
builder.WriteString("{")
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 pythonBoolLiteral(val).(string)
case int, int64, float64:
return fmt.Sprintf("%v", val)
case map[string]any:
return toPythonDict(val)
case []any:
var sliceBuilder strings.Builder
sliceBuilder.WriteString("[")
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 "None"
}
return strings.ReplaceAll(string(jsonBytes), "null", "None")
}
}
func (g *PythonGenerator) Generate(params *generators.Params[Params]) error {
funcs := template.FuncMap{
"OpenFeatureType": openFeatureType,
"TypedGetMethodSync": typedGetMethodSync,
"TypedGetMethodAsync": typedGetMethodAsync,
"TypedDetailsMethodSync": typedDetailsMethodSync,
"TypedDetailsMethodAsync": typedDetailsMethodAsync,
"PythonBoolLiteral": pythonBoolLiteral,
"ToPythonDict": toPythonDict,
}
newParams := &generators.Params[any]{
OutputPath: params.OutputPath,
TemplatePath: params.TemplatePath,
Custom: Params{},
}
return g.GenerateFile(funcs, pythonTmpl, newParams, "openfeature.py")
}
// NewGenerator creates a generator for Python.
func NewGenerator(fs *flagset.Flagset) *PythonGenerator {
return &PythonGenerator{
CommonGenerator: *generators.NewGenerator(fs, map[flagset.FlagType]bool{}),
}
}