diff --git a/CHANGELOG.md b/CHANGELOG.md index 54918a8..894b13c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Change Log +## [0.9.0] - 2017-06-06 +### Added +- add to enum methods +### Fixed +- improve MarshalJSON, UnmarshalJSON and String methods + + ## [0.8.0] - 2017-06-05 ### Added - add `String()` to enum @@ -113,6 +120,7 @@ - pre-release +[0.9.0]: https://github.com/aaharu/schemarshal/compare/0.8.0...0.9.0 [0.8.0]: https://github.com/aaharu/schemarshal/compare/0.7.3...0.8.0 [0.7.3]: https://github.com/aaharu/schemarshal/compare/0.7.2...0.7.3 [0.7.2]: https://github.com/aaharu/schemarshal/compare/0.7.1...0.7.2 diff --git a/README.md b/README.md index 42a1cb4..b836722 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ curl -s "https://raw.githubusercontent.com/aaharu/schemarshal/master/test_data/d schemarshal -p sample test_data/a.json ``` ```go -// Code generated by schemarshal 0.8.0 `schemarshal -p sample test_data/a.json` +// Code generated by schemarshal 0.9.0 `schemarshal -p sample test_data/a.json` // DO NOT RECOMMEND EDITING THIS FILE. package sample @@ -93,13 +93,14 @@ const ( APhoneNumberHogeBbbEnum6 ) +var _aPhoneNumberHogeBbbEnum = map[APhoneNumberHogeBbbEnum]interface{}{ + APhoneNumberHogeBbbEnum2: 2, + APhoneNumberHogeBbbEnum4: 4, + APhoneNumberHogeBbbEnum6: 6, +} + func (enum APhoneNumberHogeBbbEnum) MarshalJSON() ([]byte, error) { - var enumList = []interface{}{ - 2, - 4, - 6, - } - switch v := enumList[enum].(type) { + switch v := _aPhoneNumberHogeBbbEnum[enum].(type) { case string: return []byte(strconv.Quote(v)), nil default: @@ -108,12 +109,7 @@ func (enum APhoneNumberHogeBbbEnum) MarshalJSON() ([]byte, error) { } func (enum *APhoneNumberHogeBbbEnum) UnmarshalJSON(data []byte) error { - var enumList = []interface{}{ - 2, - 4, - 6, - } - for i, v := range enumList { + for i, v := range _aPhoneNumberHogeBbbEnum { switch vv := v.(type) { case string: if strconv.Quote(vv) == string(data) { @@ -130,13 +126,31 @@ func (enum *APhoneNumberHogeBbbEnum) UnmarshalJSON(data []byte) error { return fmt.Errorf("Error: miss-matched APhoneNumberHogeBbbEnum (%s)", data) } +func (enum APhoneNumberHogeBbbEnum) String() string { + switch v := _aPhoneNumberHogeBbbEnum[enum].(type) { + case string: + return v + default: + return fmt.Sprintf("%v", v) + } +} + +func ToAPhoneNumberHogeBbbEnum(val interface{}) (APhoneNumberHogeBbbEnum, error) { + for i, v := range _aPhoneNumberHogeBbbEnum { + if val == v { + return i, nil + } + } + return 0, fmt.Errorf("Error: Failed to APhoneNumberHogeBbbEnum: %v", val) +} + ``` ```bash curl -s "https://raw.githubusercontent.com/aaharu/schemarshal/master/test_data/disk.json" | schemarshal ``` ```go -// Code generated by schemarshal 0.8.0 `schemarshal` +// Code generated by schemarshal 0.9.0 `schemarshal` // DO NOT RECOMMEND EDITING THIS FILE. package main @@ -159,12 +173,13 @@ const ( SchemarshalTypeTestEnumY ) +var _schemarshalTypeTestEnum = map[SchemarshalTypeTestEnum]interface{}{ + SchemarshalTypeTestEnumX: "x", + SchemarshalTypeTestEnumY: "y", +} + func (enum SchemarshalTypeTestEnum) MarshalJSON() ([]byte, error) { - var enumList = []interface{}{ - "x", - "y", - } - switch v := enumList[enum].(type) { + switch v := _schemarshalTypeTestEnum[enum].(type) { case string: return []byte(strconv.Quote(v)), nil default: @@ -173,11 +188,7 @@ func (enum SchemarshalTypeTestEnum) MarshalJSON() ([]byte, error) { } func (enum *SchemarshalTypeTestEnum) UnmarshalJSON(data []byte) error { - var enumList = []interface{}{ - "x", - "y", - } - for i, v := range enumList { + for i, v := range _schemarshalTypeTestEnum { switch vv := v.(type) { case string: if strconv.Quote(vv) == string(data) { @@ -195,10 +206,21 @@ func (enum *SchemarshalTypeTestEnum) UnmarshalJSON(data []byte) error { } func (enum SchemarshalTypeTestEnum) String() string { - var enumList = map[int]string{} - enumList[0] = "x" - enumList[1] = "y" - return enumList[int(enum)] + switch v := _schemarshalTypeTestEnum[enum].(type) { + case string: + return v + default: + return fmt.Sprintf("%v", v) + } +} + +func ToSchemarshalTypeTestEnum(val interface{}) (SchemarshalTypeTestEnum, error) { + for i, v := range _schemarshalTypeTestEnum { + if val == v { + return i, nil + } + } + return 0, fmt.Errorf("Error: Failed to SchemarshalTypeTestEnum: %v", val) } type SchemarshalTypeTypeEnum int @@ -209,13 +231,14 @@ const ( SchemarshalTypeTypeEnumABC ) +var _schemarshalTypeTypeEnum = map[SchemarshalTypeTypeEnum]interface{}{ + SchemarshalTypeTypeEnumDisk: "disk", + SchemarshalTypeTypeEnumDisk2: "disk2", + SchemarshalTypeTypeEnumABC: "a\"b\\c", +} + func (enum SchemarshalTypeTypeEnum) MarshalJSON() ([]byte, error) { - var enumList = []interface{}{ - "disk", - "disk2", - "a\"b\\c", - } - switch v := enumList[enum].(type) { + switch v := _schemarshalTypeTypeEnum[enum].(type) { case string: return []byte(strconv.Quote(v)), nil default: @@ -224,12 +247,7 @@ func (enum SchemarshalTypeTypeEnum) MarshalJSON() ([]byte, error) { } func (enum *SchemarshalTypeTypeEnum) UnmarshalJSON(data []byte) error { - var enumList = []interface{}{ - "disk", - "disk2", - "a\"b\\c", - } - for i, v := range enumList { + for i, v := range _schemarshalTypeTypeEnum { switch vv := v.(type) { case string: if strconv.Quote(vv) == string(data) { @@ -247,11 +265,21 @@ func (enum *SchemarshalTypeTypeEnum) UnmarshalJSON(data []byte) error { } func (enum SchemarshalTypeTypeEnum) String() string { - var enumList = map[int]string{} - enumList[2] = "a\"b\\c" - enumList[0] = "disk" - enumList[1] = "disk2" - return enumList[int(enum)] + switch v := _schemarshalTypeTypeEnum[enum].(type) { + case string: + return v + default: + return fmt.Sprintf("%v", v) + } +} + +func ToSchemarshalTypeTypeEnum(val interface{}) (SchemarshalTypeTypeEnum, error) { + for i, v := range _schemarshalTypeTypeEnum { + if val == v { + return i, nil + } + } + return 0, fmt.Errorf("Error: Failed to SchemarshalTypeTypeEnum: %v", val) } ``` diff --git a/codegen/generator.go b/codegen/generator.go index c8cac4a..ed9e42f 100644 --- a/codegen/generator.go +++ b/codegen/generator.go @@ -10,6 +10,7 @@ import ( "go/format" "sort" "strconv" + "strings" "github.com/aaharu/schemarshal/utils" "github.com/aaharu/schemarshal/version" @@ -108,10 +109,15 @@ func (g *Generator) Generate() ([]byte, error) { buf.WriteString(utils.UpperCamelCase(typeName+" "+fmt.Sprintf("%v", enum[i])) + "\n") } } - buf.WriteString(")\n") - buf.WriteString("func (enum " + typeName + ") MarshalJSON() ([]byte, error) {\n") - buf.WriteString("var enumList = []interface{}{\n") + buf.WriteString(")\n\n") + + var enumMapName = "_" + strings.ToLower(typeName[:1]) + if len(typeName) > 1 { + enumMapName += typeName[1:] + } + buf.WriteString("var " + enumMapName + " = map[" + typeName + "]interface{}{\n") for i := range enum { + buf.WriteString(utils.UpperCamelCase(typeName+" "+fmt.Sprintf("%v", enum[i])) + ": ") switch v := enum[i].(type) { case string: buf.WriteString(strconv.Quote(v)) @@ -120,29 +126,19 @@ func (g *Generator) Generate() ([]byte, error) { } buf.WriteString(",\n") } - buf.WriteString("}\n") - buf.WriteString("switch v:= enumList[enum].(type) {\n") + buf.WriteString("}\n\n") + + buf.WriteString("func (enum " + typeName + ") MarshalJSON() ([]byte, error) {\n") + buf.WriteString("switch v:= " + enumMapName + "[enum].(type) {\n") buf.WriteString("case string:\n") buf.WriteString("return []byte(strconv.Quote(v)), nil\n") buf.WriteString("default:\n") buf.WriteString("return []byte(fmt.Sprintf(\"%v\", v)), nil\n") buf.WriteString("}\n") buf.WriteString("}\n\n") - var stringers = map[int]string{} + buf.WriteString("func (enum *" + typeName + ") UnmarshalJSON(data []byte) error {\n") - buf.WriteString("var enumList = []interface{}{\n") - for i := range enum { - switch v := enum[i].(type) { - case string: - buf.WriteString(strconv.Quote(v)) - stringers[i] = strconv.Quote(v) - default: - buf.WriteString(fmt.Sprintf("%v", v)) - } - buf.WriteString(",\n") - } - buf.WriteString("}\n") - buf.WriteString("for i, v := range enumList {\n") + buf.WriteString("for i, v := range " + enumMapName + " {\n") buf.WriteString("switch vv := v.(type) {\n") buf.WriteString("case string:\n") buf.WriteString("if strconv.Quote(vv) == string(data) {\n") @@ -158,15 +154,24 @@ func (g *Generator) Generate() ([]byte, error) { buf.WriteString("}\n") buf.WriteString("return fmt.Errorf(\"Error: miss-matched " + typeName + " (%s)\", data)\n") buf.WriteString("}\n\n") - if len(stringers) > 0 { - buf.WriteString("func (enum " + typeName + ") String() string {\n") - buf.WriteString("var enumList = map[int]string{}\n") - for k, v := range stringers { - buf.WriteString("enumList[" + strconv.Itoa(k) + "] = " + v + "\n") - } - buf.WriteString("return enumList[int(enum)]\n") - buf.WriteString("}\n\n") - } + + buf.WriteString("func (enum " + typeName + ") String() string {\n") + buf.WriteString("switch v:= " + enumMapName + "[enum].(type) {\n") + buf.WriteString("case string:\n") + buf.WriteString("return v\n") + buf.WriteString("default:\n") + buf.WriteString("return fmt.Sprintf(\"%v\", v)\n") + buf.WriteString("}\n") + buf.WriteString("}\n\n") + + buf.WriteString("func To" + typeName + "(val interface{}) (" + typeName + ", error) {\n") + buf.WriteString("for i, v := range " + enumMapName + " {\n") + buf.WriteString("if val == v {") + buf.WriteString("return i, nil") + buf.WriteString("}\n") + buf.WriteString("}\n") + buf.WriteString("return 0, fmt.Errorf(\"Error: Failed to " + typeName + ": %v\", val)") + buf.WriteString("}\n\n") } } diff --git a/version/version.go b/version/version.go index 21e2e61..cad64d1 100644 --- a/version/version.go +++ b/version/version.go @@ -7,7 +7,7 @@ package version import "fmt" // Version of schemarshal -const Version = "0.8.0" +const Version = "0.9.0" // String return ` ` func String() string { diff --git a/version/version_test.go b/version/version_test.go index 3892503..ee7a429 100644 --- a/version/version_test.go +++ b/version/version_test.go @@ -8,7 +8,7 @@ import "testing" func TestString(t *testing.T) { actual := String() - expected := "schemarshal 0.8.0" + expected := "schemarshal 0.9.0" if actual != expected { t.Errorf("got %v\nwant %v", actual, expected) }