diff --git a/README.md b/README.md index 2ec7e45..ec8a2c6 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,8 @@ Flags: transform each item name by adding a prefix. Default: "" -comment value comments to include in generated code, can repeat. Default: "" + -dynamodbav + if true, DynamoDB attribute values marshaling methods will be generated. Default: false -gqlgen if true, GraphQL marshaling methods for gqlgen will be generated. Default: false -json diff --git a/dynamodbav.go b/dynamodbav.go new file mode 100644 index 0000000..1c12bd5 --- /dev/null +++ b/dynamodbav.go @@ -0,0 +1,24 @@ +package main + +const dynamodbAvMethods = ` +// MarshalDynamoDBAttributeValue implements the attributevalue.Marshaler interface for %[1]s +func (i %[1]s) MarshalDynamoDBAttributeValue() (types.AttributeValue, error) { + return &types.AttributeValueMemberS{Value: i.String()}, nil +} + +// UnmarshalDynamoDBAttributeValue implements the attributevalue.Unmarshaler interface for %[1]s +func (i *%[1]s) UnmarshalDynamoDBAttributeValue(value types.AttributeValue) error { + avS, ok := value.(*types.AttributeValueMemberS) + if !ok { + return fmt.Errorf("%[1]s should be a AttributeValueMemberS, got %%T", value) + } + + var err error + *i, err = %[1]sString(avS.Value) + return err +} +` + +func (g *Generator) buildDynamodbAvMethods(runs [][]Value, typeName string) { + g.Printf(dynamodbAvMethods, typeName) +} diff --git a/golden_test.go b/golden_test.go index 03479d2..abbc078 100644 --- a/golden_test.go +++ b/golden_test.go @@ -52,6 +52,10 @@ var goldenGQLGen = []Golden{ {"primeGQLGen", primeGQLGenIn}, } +var goldenDynamodbAv = []Golden{ + {"primeDynamodbAv", primeDynamodbAv}, +} + var goldenJSONAndSQL = []Golden{ {"primeJsonAndSql", primeJsonAndSqlIn}, } @@ -258,6 +262,25 @@ const ( ) ` +const primeDynamodbAv = `type Prime int +const ( + p2 Prime = 2 + p3 Prime = 3 + p5 Prime = 5 + p7 Prime = 7 + p77 Prime = 7 // Duplicate; note that p77 doesn't appear below. + p11 Prime = 11 + p13 Prime = 13 + p17 Prime = 17 + p19 Prime = 19 + p23 Prime = 23 + p29 Prime = 29 + p37 Prime = 31 + p41 Prime = 41 + p43 Prime = 43 +) +` + const primeJsonAndSqlIn = `type Prime int const ( p2 Prime = 2 @@ -315,45 +338,48 @@ const ( func TestGolden(t *testing.T) { for _, test := range golden { - runGoldenTest(t, test, false, false, false, false, false, false, true, "", "") + runGoldenTest(t, test, false, false, false, false, false, false, false, true, "", "") } for _, test := range goldenJSON { - runGoldenTest(t, test, true, false, false, false, false, false, false, "", "") + runGoldenTest(t, test, true, false, false, false, false, false, false, false, "", "") } for _, test := range goldenText { - runGoldenTest(t, test, false, false, false, true, false, false, false, "", "") + runGoldenTest(t, test, false, false, false, true, false, false, false, false, "", "") } for _, test := range goldenYAML { - runGoldenTest(t, test, false, true, false, false, false, false, false, "", "") + runGoldenTest(t, test, false, true, false, false, false, false, false, false, "", "") } for _, test := range goldenSQL { - runGoldenTest(t, test, false, false, true, false, false, false, false, "", "") + runGoldenTest(t, test, false, false, true, false, false, false, false, false, "", "") } for _, test := range goldenJSONAndSQL { - runGoldenTest(t, test, true, false, true, false, false, false, false, "", "") + runGoldenTest(t, test, true, false, true, false, false, false, false, false, "", "") } for _, test := range goldenGQLGen { - runGoldenTest(t, test, false, false, false, false, false, true, false, "", "") + runGoldenTest(t, test, false, false, false, false, false, true, false, false, "", "") + } + for _, test := range goldenDynamodbAv { + runGoldenTest(t, test, false, false, false, false, false, false, true, false, "", "") } for _, test := range goldenTrimPrefix { - runGoldenTest(t, test, false, false, false, false, false, false, false, "Day", "") + runGoldenTest(t, test, false, false, false, false, false, false, false, false, "Day", "") } for _, test := range goldenTrimPrefixMultiple { - runGoldenTest(t, test, false, false, false, false, false, false, false, "Day,Night", "") + runGoldenTest(t, test, false, false, false, false, false, false, false, false, "Day,Night", "") } for _, test := range goldenWithPrefix { - runGoldenTest(t, test, false, false, false, false, false, false, false, "", "Day") + runGoldenTest(t, test, false, false, false, false, false, false, false, false, "", "Day") } for _, test := range goldenTrimAndAddPrefix { - runGoldenTest(t, test, false, false, false, false, false, false, false, "Day", "Night") + runGoldenTest(t, test, false, false, false, false, false, false, false, false, "Day", "Night") } for _, test := range goldenLinecomment { - runGoldenTest(t, test, false, false, false, false, true, false, false, "", "") + runGoldenTest(t, test, false, false, false, false, true, false, false, false, "", "") } } func runGoldenTest(t *testing.T, test Golden, - generateJSON, generateYAML, generateSQL, generateText, linecomment, generateGQLGen, generateValuesMethod bool, + generateJSON, generateYAML, generateSQL, generateText, linecomment, generateGQLGen, generateDynamodbAv, generateValuesMethod bool, trimPrefix string, prefix string) { var g Generator @@ -382,10 +408,10 @@ func runGoldenTest(t *testing.T, test Golden, if len(tokens) != 3 { t.Fatalf("%s: need type declaration on first line", test.name) } - g.generate(tokens[1], generateJSON, generateYAML, generateSQL, generateText, generateGQLGen, "noop", trimPrefix, prefix, linecomment, generateValuesMethod) + g.generate(tokens[1], generateJSON, generateYAML, generateSQL, generateText, generateGQLGen, generateDynamodbAv, "noop", trimPrefix, prefix, linecomment, generateValuesMethod) got := string(g.format()) if got != loadGolden(test.name) { - // Use this to help build a golden text when changes are needed + //Use this to help build a golden text when changes are needed //goldenFile := fmt.Sprintf("./testdata/%v.golden", test.name) //err = ioutil.WriteFile(goldenFile, []byte(got), 0644) //if err != nil { diff --git a/gqlgen.go b/gqlgen.go index 45dddbb..ba24a5f 100644 --- a/gqlgen.go +++ b/gqlgen.go @@ -1,23 +1,24 @@ package main // Arguments to format are: +// // [1]: type name const gqlgenMethods = ` // MarshalGQL implements the graphql.Marshaler interface for %[1]s func (i %[1]s) MarshalGQL(w io.Writer) { - fmt.Fprint(w, strconv.Quote(i.String())) + fmt.Fprint(w, strconv.Quote(i.String())) } // UnmarshalGQL implements the graphql.Unmarshaler interface for %[1]s func (i *%[1]s) UnmarshalGQL(value interface{}) error { - str, ok := value.(string) - if !ok { - return fmt.Errorf("%[1]s should be a string, got %%T", value) - } + str, ok := value.(string) + if !ok { + return fmt.Errorf("%[1]s should be a string, got %%T", value) + } - var err error - *i, err = %[1]sString(str) - return err + var err error + *i, err = %[1]sString(str) + return err } ` diff --git a/stringer.go b/stringer.go index 49c87fd..289d61e 100644 --- a/stringer.go +++ b/stringer.go @@ -50,6 +50,7 @@ var ( yaml = flag.Bool("yaml", false, "if true, yaml marshaling methods will be generated. Default: false") text = flag.Bool("text", false, "if true, text marshaling methods will be generated. Default: false") gqlgen = flag.Bool("gqlgen", false, "if true, GraphQL marshaling methods for gqlgen will be generated. Default: false") + dynamodbav = flag.Bool("dynamodbav", false, "if true, DynamoDB attribute values marshaling methods will be generated. Default: false") altValuesFunc = flag.Bool("values", false, "if true, alternative string values method will be generated. Default: false") output = flag.String("output", "", "output file name; default srcdir/_string.go") transformMethod = flag.String("transform", "noop", "enum item name transformation method. Default: noop") @@ -131,11 +132,14 @@ func main() { g.Printf("\t\"io\"\n") g.Printf("\t\"strconv\"\n") } + if *dynamodbav { + g.Printf("\t\"github.com/aws/aws-sdk-go-v2/service/dynamodb/types\"\n") + } g.Printf(")\n") // Run generate for each type. for _, typeName := range typs { - g.generate(typeName, *json, *yaml, *sql, *text, *gqlgen, *transformMethod, *trimPrefix, *addPrefix, *linecomment, *altValuesFunc) + g.generate(typeName, *json, *yaml, *sql, *text, *gqlgen, *dynamodbav, *transformMethod, *trimPrefix, *addPrefix, *linecomment, *altValuesFunc) } // Format the output. @@ -414,7 +418,7 @@ func (g *Generator) prefixValueNames(values []Value, prefix string) { // generate produces the String method for the named type. func (g *Generator) generate(typeName string, - includeJSON, includeYAML, includeSQL, includeText, includeGQLGen bool, + includeJSON, includeYAML, includeSQL, includeText, includeGQLGen bool, includeDynamodbav bool, transformMethod string, trimPrefix string, addPrefix string, lineComment bool, includeValuesMethod bool) { values := make([]Value, 0, 100) for _, file := range g.pkg.files { @@ -484,6 +488,9 @@ func (g *Generator) generate(typeName string, if includeGQLGen { g.buildGQLGenMethods(runs, typeName) } + if includeDynamodbav { + g.buildDynamodbAvMethods(runs, typeName) + } } // splitIntoRuns breaks the values into runs of contiguous sequences. @@ -774,9 +781,10 @@ func (g *Generator) buildOneRun(runs [][]Value, typeName string) { } // Arguments to format are: -// [1]: type name -// [2]: size of index element (8 for uint8 etc.) -// [3]: less than zero check (for signed types) +// +// [1]: type name +// [2]: size of index element (8 for uint8 etc.) +// [3]: less than zero check (for signed types) const stringOneRun = `func (i %[1]s) String() string { if %[3]si >= %[1]s(len(_%[1]sIndex)-1) { return fmt.Sprintf("%[1]s(%%d)", i) @@ -786,10 +794,11 @@ const stringOneRun = `func (i %[1]s) String() string { ` // Arguments to format are: -// [1]: type name -// [2]: lowest defined value for type, as a string -// [3]: size of index element (8 for uint8 etc.) -// [4]: less than zero check (for signed types) +// +// [1]: type name +// [2]: lowest defined value for type, as a string +// [3]: size of index element (8 for uint8 etc.) +// [4]: less than zero check (for signed types) const stringOneRunWithOffset = `func (i %[1]s) String() string { i -= %[2]s if %[4]si >= %[1]s(len(_%[1]sIndex)-1) { diff --git a/testdata/primeDynamodbAv.golden b/testdata/primeDynamodbAv.golden new file mode 100644 index 0000000..81faa7d --- /dev/null +++ b/testdata/primeDynamodbAv.golden @@ -0,0 +1,140 @@ + +const _PrimeName = "p2p3p5p7p11p13p17p19p23p29p37p41p43" +const _PrimeLowerName = "p2p3p5p7p11p13p17p19p23p29p37p41p43" + +var _PrimeMap = map[Prime]string{ + 2: _PrimeName[0:2], + 3: _PrimeName[2:4], + 5: _PrimeName[4:6], + 7: _PrimeName[6:8], + 11: _PrimeName[8:11], + 13: _PrimeName[11:14], + 17: _PrimeName[14:17], + 19: _PrimeName[17:20], + 23: _PrimeName[20:23], + 29: _PrimeName[23:26], + 31: _PrimeName[26:29], + 41: _PrimeName[29:32], + 43: _PrimeName[32:35], +} + +func (i Prime) String() string { + if str, ok := _PrimeMap[i]; ok { + return str + } + return fmt.Sprintf("Prime(%d)", i) +} + +// An "invalid array index" compiler error signifies that the constant values have changed. +// Re-run the stringer command to generate them again. +func _PrimeNoOp() { + var x [1]struct{} + _ = x[p2-(2)] + _ = x[p3-(3)] + _ = x[p5-(5)] + _ = x[p7-(7)] + _ = x[p11-(11)] + _ = x[p13-(13)] + _ = x[p17-(17)] + _ = x[p19-(19)] + _ = x[p23-(23)] + _ = x[p29-(29)] + _ = x[p37-(31)] + _ = x[p41-(41)] + _ = x[p43-(43)] +} + +var _PrimeValues = []Prime{p2, p3, p5, p7, p11, p13, p17, p19, p23, p29, p37, p41, p43} + +var _PrimeNameToValueMap = map[string]Prime{ + _PrimeName[0:2]: p2, + _PrimeLowerName[0:2]: p2, + _PrimeName[2:4]: p3, + _PrimeLowerName[2:4]: p3, + _PrimeName[4:6]: p5, + _PrimeLowerName[4:6]: p5, + _PrimeName[6:8]: p7, + _PrimeLowerName[6:8]: p7, + _PrimeName[8:11]: p11, + _PrimeLowerName[8:11]: p11, + _PrimeName[11:14]: p13, + _PrimeLowerName[11:14]: p13, + _PrimeName[14:17]: p17, + _PrimeLowerName[14:17]: p17, + _PrimeName[17:20]: p19, + _PrimeLowerName[17:20]: p19, + _PrimeName[20:23]: p23, + _PrimeLowerName[20:23]: p23, + _PrimeName[23:26]: p29, + _PrimeLowerName[23:26]: p29, + _PrimeName[26:29]: p37, + _PrimeLowerName[26:29]: p37, + _PrimeName[29:32]: p41, + _PrimeLowerName[29:32]: p41, + _PrimeName[32:35]: p43, + _PrimeLowerName[32:35]: p43, +} + +var _PrimeNames = []string{ + _PrimeName[0:2], + _PrimeName[2:4], + _PrimeName[4:6], + _PrimeName[6:8], + _PrimeName[8:11], + _PrimeName[11:14], + _PrimeName[14:17], + _PrimeName[17:20], + _PrimeName[20:23], + _PrimeName[23:26], + _PrimeName[26:29], + _PrimeName[29:32], + _PrimeName[32:35], +} + +// PrimeString retrieves an enum value from the enum constants string name. +// Throws an error if the param is not part of the enum. +func PrimeString(s string) (Prime, error) { + if val, ok := _PrimeNameToValueMap[s]; ok { + return val, nil + } + + if val, ok := _PrimeNameToValueMap[strings.ToLower(s)]; ok { + return val, nil + } + return 0, fmt.Errorf("%s does not belong to Prime values", s) +} + +// PrimeValues returns all values of the enum +func PrimeValues() []Prime { + return _PrimeValues +} + +// PrimeStrings returns a slice of all String values of the enum +func PrimeStrings() []string { + strs := make([]string, len(_PrimeNames)) + copy(strs, _PrimeNames) + return strs +} + +// IsAPrime returns "true" if the value is listed in the enum definition. "false" otherwise +func (i Prime) IsAPrime() bool { + _, ok := _PrimeMap[i] + return ok +} + +// MarshalDynamoDBAttributeValue implements the attributevalue.Marshaler interface for Prime +func (i Prime) MarshalDynamoDBAttributeValue() (types.AttributeValue, error) { + return &types.AttributeValueMemberS{Value: i.String()}, nil +} + +// UnmarshalDynamoDBAttributeValue implements the attributevalue.Unmarshaler interface for Prime +func (i *Prime) UnmarshalDynamoDBAttributeValue(value types.AttributeValue) error { + avS, ok := value.(*types.AttributeValueMemberS) + if !ok { + return fmt.Errorf("Prime should be a AttributeValueMemberS, got %T", value) + } + + var err error + *i, err = PrimeString(avS.Value) + return err +}