Skip to content

Commit

Permalink
0.4.2
Browse files Browse the repository at this point in the history
fixed
- enum name bugs
  • Loading branch information
aaharu committed Feb 19, 2017
1 parent 7e3f70f commit d465e5b
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 27 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## [0.4.2] - 2017-02-19
### Fixed
- enum name bugs

## [0.4.1] - 2017-02-19
### Fixed
- enum in array bugs
Expand Down Expand Up @@ -49,6 +53,7 @@
- pre-release


[0.4.2]: https://github.com/aaharu/schemarshal/compare/0.4.1...0.4.2
[0.4.1]: https://github.com/aaharu/schemarshal/compare/0.4.0...0.4.1
[0.4.0]: https://github.com/aaharu/schemarshal/compare/0.3.0...0.4.0
[0.3.0]: https://github.com/aaharu/schemarshal/compare/0.2.1...0.3.0
Expand Down
73 changes: 54 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Generates Go struct types from a [JSON Schema](http://json-schema.org/).
## Installation

```bash
go get github.com/aaharu/schemarshal
go get -u github.com/aaharu/schemarshal
```

## Usage
Expand All @@ -30,6 +30,14 @@ OPTIONS
Show version.
```

```bash
# with args
schemarshal -o api/schema/gen.go -p gen -f schema.json

# pipe
curl -s "https://raw.githubusercontent.com/aaharu/schemarshal/master/test_data/disk.json" | schemarshal
```

## TODO

- [ ] use go/ast
Expand All @@ -40,28 +48,55 @@ OPTIONS
schemarshal -p sample test_data/a.json
```
```go
// generated by schemarshal 0.4.1 `./schemarshal -p sample test_data/a.json`
// generated by schemarshal 0.4.2 `schemarshal -p sample test_data/a.json`
// DO NOT RECOMMEND EDITING THIS FILE.

package sample

import "time"
import (
"fmt"
"strconv"
"time"
)

type A struct {
Test []int `json:"test,omitempty"`
Test2 time.Time `json:"test2,omitempty"`
Test time.Time `json:"test2,omitempty"`
PhoneNumber []struct {
Location string `json:"location"`
Code int `json:"code"`
Hoge struct {
Aaa bool `json:"aaa,omitempty"`
Aaa bool `json:"aaa,omitempty"`
Bbb APhoneNumberHogeBbbEnum `json:"bbb,omitempty"`
} `json:"hoge,omitempty"`
Fuga map[string]interface{} `json:"fuga,omitempty"`
} `json:"phoneNumber"`
Address struct {
StreetAddress string `json:"streetAddress"`
City *string `json:"city"`
} `json:"address"`
Test []int `json:"test,omitempty"`
}

type APhoneNumberHogeBbbEnum int

const (
APhoneNumberHogeBbbEnum2 APhoneNumberHogeBbbEnum = iota
APhoneNumberHogeBbbEnum4
APhoneNumberHogeBbbEnum6
)

func (enum APhoneNumberHogeBbbEnum) MarshalJSON() ([]byte, error) {
var enumList = []interface{}{
2,
4,
6,
}
switch v := enumList[enum].(type) {
case string:
return []byte(strconv.Quote(v)), nil
default:
return []byte(fmt.Sprintf("%v", v)), nil
}
}

```
Expand All @@ -70,7 +105,7 @@ type A struct {
curl -s "https://raw.githubusercontent.com/aaharu/schemarshal/master/test_data/disk.json" | schemarshal
```
```go
// generated by schemarshal 0.4.1 `schemarshal`
// generated by schemarshal 0.4.2 `schemarshal`
// DO NOT RECOMMEND EDITING THIS FILE.

package main
Expand All @@ -81,20 +116,20 @@ import (
)

type Disk struct {
Type TypeEnum `json:"type"`
Device string `json:"device"`
Test []TestEnum `json:"test,omitempty"`
Type DiskTypeEnum `json:"type"`
Device string `json:"device"`
Test []DiskTestEnum `json:"test,omitempty"`
}

type TypeEnum int
type DiskTypeEnum int

const (
TypeEnumDisk TypeEnum = iota
TypeEnumDisk2
TypeEnumABC
DiskTypeEnumDisk DiskTypeEnum = iota
DiskTypeEnumDisk2
DiskTypeEnumABC
)

func (enum TypeEnum) MarshalJSON() ([]byte, error) {
func (enum DiskTypeEnum) MarshalJSON() ([]byte, error) {
var enumList = []interface{}{
"disk",
"disk2",
Expand All @@ -108,14 +143,14 @@ func (enum TypeEnum) MarshalJSON() ([]byte, error) {
}
}

type TestEnum int
type DiskTestEnum int

const (
TestEnumX TestEnum = iota
TestEnumY
DiskTestEnumX DiskTestEnum = iota
DiskTestEnumY
)

func (enum TestEnum) MarshalJSON() ([]byte, error) {
func (enum DiskTestEnum) MarshalJSON() ([]byte, error) {
var enumList = []interface{}{
"x",
"y",
Expand Down
12 changes: 6 additions & 6 deletions codegen/jsonschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (js *JSONSchema) Parse(fieldName string) (*JSONType, EnumSpec, ImportSpec,
if _, ok := enumList[enumName]; ok == true {
// FIXME: unsupported
err := fmt.Errorf("unsupported json")
return t, enumList, imports, err
return nil, nil, nil, err
}
enumList[enumName] = js.schema.Enum
t.enumType = enumName
Expand Down Expand Up @@ -106,7 +106,7 @@ func (js *JSONSchema) Parse(fieldName string) (*JSONType, EnumSpec, ImportSpec,
}
if js.schema.Properties != nil {
for key, propSchema := range js.schema.Properties {
propType, propEnumList, propImports, err := NewSchema(propSchema).Parse(utils.UpperCamelCase(key))
propType, propEnumList, propImports, err := NewSchema(propSchema).Parse(utils.UpperCamelCase(fieldName + " " + key))
if err != nil {
return nil, nil, nil, err
}
Expand All @@ -122,7 +122,7 @@ func (js *JSONSchema) Parse(fieldName string) (*JSONType, EnumSpec, ImportSpec,
if _, ok := enumList[k]; ok == true {
// FIXME: unsupported
err := fmt.Errorf("unsupported json")
return t, enumList, imports, err
return nil, nil, nil, err
}
enumList[k] = v
}
Expand All @@ -144,7 +144,7 @@ func (js *JSONSchema) Parse(fieldName string) (*JSONType, EnumSpec, ImportSpec,
if js.schema.Items.TupleMode {
// unsupported
err := fmt.Errorf("unsupported type %v", js.schema.Items)
return t, enumList, imports, err
return nil, nil, nil, err
}
t.format = formatArray
if inPrimitiveTypes(schema.NullType, js.schema.Type) {
Expand All @@ -159,7 +159,7 @@ func (js *JSONSchema) Parse(fieldName string) (*JSONType, EnumSpec, ImportSpec,
if _, ok := enumList[k]; ok == true {
// FIXME: unsupported
err := fmt.Errorf("unsupported json")
return t, enumList, imports, err
return nil, nil, nil, err
}
enumList[k] = v
imports[`"strconv"`] = ""
Expand All @@ -171,7 +171,7 @@ func (js *JSONSchema) Parse(fieldName string) (*JSONType, EnumSpec, ImportSpec,
return t, enumList, imports, nil
}
err := fmt.Errorf("unsupported type %v", js.schema.Type)
return t, enumList, imports, err
return nil, nil, nil, err
}

func inPrimitiveTypes(needle schema.PrimitiveType, haystack schema.PrimitiveTypes) bool {
Expand Down
4 changes: 4 additions & 0 deletions test_data/a.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
"properties": {
"aaa": {
"type": "boolean"
},
"bbb": {
"type": "integer",
"enum": [ 2, 4, 6 ]
}
}
},
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.4.1"
const Version = "0.4.2"

// 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.4.1"
expected := "schemarshal 0.4.2"
if actual != expected {
t.Errorf("got %v\nwant %v", actual, expected)
}
Expand Down

0 comments on commit d465e5b

Please sign in to comment.