Skip to content

Commit 558e964

Browse files
dominikhaskaDominik Haska
andauthored
feat: Fixing problems with generated code for golang and adding sample manifest for testing. (#22)
<!-- Please use this template for your pull request. --> <!-- Please use the sections that you need and delete other sections --> ## This PR <!-- add the description of the PR here --> Several small changes: - Add sample manifest - Fix integer parsing and generating for golang - Small fixes for missing defaults in switches ### Related Issues No issue - I've just tried to use the code to generate accessors --------- Signed-off-by: Dominik Haska <[email protected]> Co-authored-by: Dominik Haska <[email protected]>
1 parent fbac8ce commit 558e964

File tree

4 files changed

+53
-10
lines changed

4 files changed

+53
-10
lines changed

sample/sample_manifest.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"flags": {
3+
"enableFeatureA": {
4+
"flagType": "boolean",
5+
"defaultValue": false,
6+
"description": "Controls whether Feature A is enabled."
7+
},
8+
"usernameMaxLength": {
9+
"flagType": "integer",
10+
"defaultValue": 50,
11+
"description": "Maximum allowed length for usernames."
12+
},
13+
"greetingMessage": {
14+
"flagType": "string",
15+
"defaultValue": "Hello there!",
16+
"description": "The message to use for greeting users."
17+
},
18+
"discountPercentage": {
19+
"flagType": "float",
20+
"defaultValue": 0.15,
21+
"description": "Discount percentage applied to purchases."
22+
},
23+
"themeCustomization": {
24+
"flagType": "object",
25+
"defaultValue": {
26+
"primaryColor": "#007bff",
27+
"secondaryColor": "#6c757d"
28+
},
29+
"description": "Allows customization of theme colors."
30+
}
31+
}
32+
}

src/generators/golang/golang.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package golang
22

33
import (
4-
generator "codegen/src/generators"
54
_ "embed"
65
"sort"
76
"strconv"
87
"text/template"
98

9+
generator "codegen/src/generators"
10+
1011
"github.com/iancoleman/strcase"
1112
)
1213

@@ -63,8 +64,9 @@ func providerType(t generator.FlagType) string {
6364
return "BooleanProvider"
6465
case generator.StringType:
6566
return "StringProvider"
67+
default:
68+
return ""
6669
}
67-
return ""
6870
}
6971

7072
func flagAccessFunc(t generator.FlagType) string {
@@ -77,8 +79,9 @@ func flagAccessFunc(t generator.FlagType) string {
7779
return "BooleanValue"
7880
case generator.StringType:
7981
return "StringValue"
82+
default:
83+
return ""
8084
}
81-
return ""
8285
}
8386

8487
func supportImports(flags []*generator.FlagTmplData) []string {
@@ -106,7 +109,7 @@ func typeString(flagType generator.FlagType) string {
106109
case generator.StringType:
107110
return "string"
108111
case generator.IntType:
109-
return "int"
112+
return "int64"
110113
case generator.BoolType:
111114
return "bool"
112115
case generator.FloatType:

src/main.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package main
22

33
import (
4-
flagmanifest "codegen/docs/schema/v0"
5-
generator "codegen/src/generators"
6-
"codegen/src/generators/golang"
74
_ "embed"
85
"encoding/json"
96
"flag"
@@ -13,6 +10,10 @@ import (
1310
"path"
1411
"strconv"
1512

13+
flagmanifest "codegen/docs/schema/v0"
14+
generator "codegen/src/generators"
15+
"codegen/src/generators/golang"
16+
1617
jsonschema "github.com/santhosh-tekuri/jsonschema/v5"
1718
)
1819

@@ -25,20 +26,25 @@ var stringToFlagType = map[string]generator.FlagType{
2526
"boolean": generator.BoolType,
2627
"float": generator.FloatType,
2728
"integer": generator.IntType,
29+
"object": generator.ObjectType,
2830
}
2931

3032
func getDefaultValue(defaultValue interface{}, flagType generator.FlagType) string {
3133
switch flagType {
3234
case generator.BoolType:
3335
return strconv.FormatBool(defaultValue.(bool))
3436
case generator.IntType:
35-
return strconv.FormatInt(defaultValue.(int64), 10)
37+
//the conversion to float64 instead of integer typically occurs
38+
//due to how JSON is parsed in Go. In Go's encoding/json package,
39+
//all JSON numbers are unmarshaled into float64 by default when decoding into an interface{}.
40+
return strconv.FormatFloat(defaultValue.(float64), 'g', -1, 64)
3641
case generator.FloatType:
3742
return strconv.FormatFloat(defaultValue.(float64), 'g', -1, 64)
3843
case generator.StringType:
3944
return defaultValue.(string)
45+
default:
46+
return ""
4047
}
41-
return ""
4248
}
4349

4450
func unmarshalFlagManifest(data []byte, supportedFlagTypes map[generator.FlagType]bool) (*generator.BaseTmplData, error) {
@@ -100,6 +106,8 @@ func generate(gen generator.Generator) error {
100106
return gen.Generate(input)
101107
}
102108

109+
// command line params working example
110+
// -flag_manifest_path "sample/sample_manifest.json" -output_path "sample/golang/golang_sample.go"
103111
func main() {
104112
flag.Parse()
105113
_, filename := path.Split(*outputPath)

src/providers/providers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ import (
66

77
type BooleanProvider func(ctx context.Context) (bool, error)
88
type FloatProvider func(ctx context.Context) (float64, error)
9-
type IntProvider func(ctx context.Context) (int, error)
9+
type IntProvider func(ctx context.Context) (int64, error)
1010
type StringProvider func(ctx context.Context) (string, error)

0 commit comments

Comments
 (0)