Skip to content

Commit

Permalink
v0.7.0
Browse files Browse the repository at this point in the history
Merge pull request #9 from aaharu/add-unmarshal
add UnmarshalJSON to enum
  • Loading branch information
aaharu authored May 8, 2017
2 parents 3e8bd49 + e3763c9 commit f114f3d
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 13 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
69 changes: 67 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 (
Expand All @@ -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
Expand Down
27 changes: 27 additions & 0 deletions codegen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

Expand Down
2 changes: 1 addition & 1 deletion version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package version
import "fmt"

// Version of schemarshal
const Version = "0.6.0"
const Version = "0.7.0"

// String return `<name> <version>`
func String() string {
Expand Down
2 changes: 1 addition & 1 deletion version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
13 changes: 4 additions & 9 deletions wercker.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
box: tcnksm/gox
box: alexeiled/go-builder
build:
steps:
- setup-go-workspace
Expand All @@ -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
Expand Down

0 comments on commit f114f3d

Please sign in to comment.