diff --git a/CHANGELOG.md b/CHANGELOG.md index b4ab978..4098f53 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Change Log +## [0.7.0] - 2017-05-09 +### Added +- add UnmarshalJSON to enum + + ## [0.6.0] - 2017-04-18 ### Changed - object struct @@ -88,6 +93,7 @@ - pre-release +[0.7.0]: https://github.com/aaharu/schemarshal/compare/0.6.0...0.7.0 [0.6.0]: https://github.com/aaharu/schemarshal/compare/0.5.0...0.6.0 [0.5.0]: https://github.com/aaharu/schemarshal/compare/0.4.6...0.5.0 [0.4.6]: https://github.com/aaharu/schemarshal/compare/0.4.5...0.4.6 diff --git a/README.md b/README.md index cabba7f..7e8f4fe 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 -// generated by schemarshal 0.6.0 `schemarshal -p sample test_data/a.json` +// generated by schemarshal 0.7.0 `schemarshal -p sample test_data/a.json` // DO NOT RECOMMEND EDITING THIS FILE. package sample @@ -107,13 +107,35 @@ func (enum APhoneNumberHogeBbbEnum) MarshalJSON() ([]byte, error) { } } +func (enum APhoneNumberHogeBbbEnum) UnmarshalJSON(data []byte) error { + var enumList = []interface{}{ + 2, + 4, + 6, + } + for i, v := range enumList { + switch vv := v.(type) { + case string: + if strconv.Quote(vv) == string(data) { + enum = APhoneNumberHogeBbbEnum(i) + return nil + } + default: + if fmt.Sprintf("%v", v) == string(data) { + enum = APhoneNumberHogeBbbEnum(i) + return nil + } + } + } +} + ``` ```bash curl -s "https://raw.githubusercontent.com/aaharu/schemarshal/master/test_data/disk.json" | schemarshal ``` ```go -// Code generated by schemarshal 0.6.0 `schemarshal` +// Code generated by schemarshal 0.7.0 `schemarshal` // DO NOT RECOMMEND EDITING THIS FILE. package main @@ -149,6 +171,27 @@ func (enum SchemarshalTypeTestEnum) MarshalJSON() ([]byte, error) { } } +func (enum SchemarshalTypeTestEnum) UnmarshalJSON(data []byte) error { + var enumList = []interface{}{ + "x", + "y", + } + for i, v := range enumList { + switch vv := v.(type) { + case string: + if strconv.Quote(vv) == string(data) { + enum = SchemarshalTypeTestEnum(i) + return nil + } + default: + if fmt.Sprintf("%v", v) == string(data) { + enum = SchemarshalTypeTestEnum(i) + return nil + } + } + } +} + type SchemarshalTypeTypeEnum int const ( @@ -171,6 +214,28 @@ 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 { + switch vv := v.(type) { + case string: + if strconv.Quote(vv) == string(data) { + enum = SchemarshalTypeTypeEnum(i) + return nil + } + default: + if fmt.Sprintf("%v", v) == string(data) { + enum = SchemarshalTypeTypeEnum(i) + return nil + } + } + } +} + ``` ## Dependencies diff --git a/codegen/generator.go b/codegen/generator.go index 4de18d7..6f9374b 100644 --- a/codegen/generator.go +++ b/codegen/generator.go @@ -128,6 +128,33 @@ func (g *Generator) Generate() ([]byte, error) { buf.WriteString("return []byte(fmt.Sprintf(\"%v\", v)), nil\n") buf.WriteString("}\n") buf.WriteString("}\n\n") + 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)) + default: + buf.WriteString(fmt.Sprintf("%v", v)) + } + buf.WriteString(",\n") + } + buf.WriteString("}\n") + buf.WriteString("for i, v := range enumList {\n") + buf.WriteString("switch vv := v.(type) {\n") + buf.WriteString("case string:\n") + buf.WriteString("if strconv.Quote(vv) == string(data) {\n") + buf.WriteString("enum = " + typeName + "(i)\n") + buf.WriteString("return nil\n") + buf.WriteString("}\n") + buf.WriteString("default:\n") + buf.WriteString("if fmt.Sprintf(\"%v\", v) == string(data) {\n") + buf.WriteString("enum = " + typeName + "(i)\n") + buf.WriteString("return nil\n") + buf.WriteString("}\n") + buf.WriteString("}\n") + buf.WriteString("}\n") + buf.WriteString("}\n\n") } } diff --git a/version/version.go b/version/version.go index ca0b58b..bbab8ba 100644 --- a/version/version.go +++ b/version/version.go @@ -7,7 +7,7 @@ package version import "fmt" // Version of schemarshal -const Version = "0.6.0" +const Version = "0.7.0" // String return ` ` func String() string { diff --git a/version/version_test.go b/version/version_test.go index d9b4bf3..e38cfdc 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.6.0" + expected := "schemarshal 0.7.0" if actual != expected { t.Errorf("got %v\nwant %v", actual, expected) } diff --git a/wercker.yml b/wercker.yml index 6bd1c11..27d100d 100644 --- a/wercker.yml +++ b/wercker.yml @@ -1,4 +1,4 @@ -box: tcnksm/gox +box: alexeiled/go-builder build: steps: - setup-go-workspace @@ -7,25 +7,20 @@ build: - script: name: glide install code: | - curl -L -o glide.tar.gz https://github.com/Masterminds/glide/releases/download/v0.12.3/glide-v0.12.3-linux-amd64.tar.gz - tar -zxf glide.tar.gz - mv linux-amd64/glide "${WERCKER_SOURCE_DIR}/glide" - rm -rf linux-amd64 glide.tar.gz - "${WERCKER_SOURCE_DIR}/glide" --version - "${WERCKER_SOURCE_DIR}/glide" install + glide install # Build the project - script: name: go build code: | - go build $("${WERCKER_SOURCE_DIR}/glide" novendor) + go build $(glide novendor) # Test the project - script: name: go test and goveralls code: | echo "mode: count" > c.out - for pkg in $("${WERCKER_SOURCE_DIR}/glide" novendor); do + for pkg in $(glide novendor); do go test -v -covermode=count -coverprofile="pkg.out" "${pkg}" if [ -f "pkg.out" ]; then tail -n +2 "pkg.out" >> c.out