Skip to content

Commit

Permalink
Merge pull request #14 from aaharu/to-enum
Browse files Browse the repository at this point in the history
add `to enum` methods
  • Loading branch information
aaharu authored Jun 5, 2017
2 parents 3b181ec + 1cabf21 commit 3f44b94
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 75 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
118 changes: 73 additions & 45 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
// 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
Expand Down Expand Up @@ -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:
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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) {
Expand All @@ -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)
}

```
Expand Down
61 changes: 33 additions & 28 deletions codegen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"go/format"
"sort"
"strconv"
"strings"

"github.com/aaharu/schemarshal/utils"
"github.com/aaharu/schemarshal/version"
Expand Down Expand Up @@ -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))
Expand All @@ -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")
Expand All @@ -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")
}
}

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.8.0"
const Version = "0.9.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.8.0"
expected := "schemarshal 0.9.0"
if actual != expected {
t.Errorf("got %v\nwant %v", actual, expected)
}
Expand Down

0 comments on commit 3f44b94

Please sign in to comment.