Merge 465b153cfd
into 7058b320c2
This commit is contained in:
commit
ab3ed1d361
@ -21,6 +21,14 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type TestCasing int
|
||||
|
||||
const (
|
||||
caseMadnessA TestCasing = iota
|
||||
caseMaDnEEsB
|
||||
normalCaseExample
|
||||
)
|
||||
|
||||
//go:generate jsonenums -type=ShirtSize
|
||||
|
||||
type ShirtSize byte
|
||||
|
78
example/testcasing_jsonenums.go
Normal file
78
example/testcasing_jsonenums.go
Normal file
@ -0,0 +1,78 @@
|
||||
// generated by jsonenums -type=TestCasing; DO NOT EDIT
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
var (
|
||||
_TestCasingNameToValue = map[string]TestCasing{
|
||||
"case_madness_a": caseMadnessA,
|
||||
"case_ma_dn_e_es_b": caseMaDnEEsB,
|
||||
"normal_case_example": normalCaseExample,
|
||||
}
|
||||
|
||||
_TestCasingValueToName = map[TestCasing]string{
|
||||
caseMadnessA: "case_madness_a",
|
||||
caseMaDnEEsB: "case_ma_dn_e_es_b",
|
||||
normalCaseExample: "normal_case_example",
|
||||
}
|
||||
)
|
||||
|
||||
func init() {
|
||||
var v TestCasing
|
||||
if _, ok := interface{}(v).(fmt.Stringer); ok {
|
||||
_TestCasingNameToValue = map[string]TestCasing{
|
||||
interface{}(caseMadnessA).(fmt.Stringer).String(): caseMadnessA,
|
||||
interface{}(caseMaDnEEsB).(fmt.Stringer).String(): caseMaDnEEsB,
|
||||
interface{}(normalCaseExample).(fmt.Stringer).String(): normalCaseExample,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MarshalJSON is generated so TestCasing satisfies json.Marshaler.
|
||||
func (r TestCasing) MarshalJSON() ([]byte, error) {
|
||||
if s, ok := interface{}(r).(fmt.Stringer); ok {
|
||||
return json.Marshal(s.String())
|
||||
}
|
||||
s, ok := _TestCasingValueToName[r]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("invalid TestCasing: %d", r)
|
||||
}
|
||||
return json.Marshal(s)
|
||||
}
|
||||
|
||||
// UnmarshalJSON is generated so TestCasing satisfies json.Unmarshaler.
|
||||
func (r *TestCasing) UnmarshalJSON(data []byte) error {
|
||||
var s string
|
||||
if err := json.Unmarshal(data, &s); err != nil {
|
||||
return fmt.Errorf("TestCasing should be a string, got %s", data)
|
||||
}
|
||||
v, ok := _TestCasingNameToValue[s]
|
||||
if !ok {
|
||||
return fmt.Errorf("invalid TestCasing %q", s)
|
||||
}
|
||||
*r = v
|
||||
return nil
|
||||
}
|
||||
|
||||
//Scan an input string into this structure for use with GORP
|
||||
func (r *TestCasing) Scan(i interface{}) error {
|
||||
switch i.(type) {
|
||||
case string:
|
||||
r.UnmarshalJSON([]byte(i.(string)))
|
||||
default:
|
||||
return errors.Errorf("Can't scan %T into type %T", i, r)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r TestCasing) Value() (driver.Value, error) {
|
||||
bytes, err := r.MarshalJSON()
|
||||
return string(bytes), err
|
||||
}
|
20
jsonenums.go
20
jsonenums.go
@ -84,7 +84,7 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/campoy/jsonenums/parser"
|
||||
"github.com/BenjaminTrapani/jsonenums/parser"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -93,6 +93,11 @@ var (
|
||||
outputSuffix = flag.String("suffix", "_jsonenums", "suffix to be added to the output file")
|
||||
)
|
||||
|
||||
type CammelSnakePair struct {
|
||||
CammelRep string
|
||||
SnakeRep string
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
if len(*typeNames) == 0 {
|
||||
@ -121,11 +126,11 @@ func main() {
|
||||
var analysis = struct {
|
||||
Command string
|
||||
PackageName string
|
||||
TypesAndValues map[string][]string
|
||||
TypesAndValues map[string][]CammelSnakePair
|
||||
}{
|
||||
Command: strings.Join(os.Args[1:], " "),
|
||||
PackageName: pkg.Name,
|
||||
TypesAndValues: make(map[string][]string),
|
||||
TypesAndValues: make(map[string][]CammelSnakePair),
|
||||
}
|
||||
|
||||
// Run generate for each type.
|
||||
@ -134,7 +139,14 @@ func main() {
|
||||
if err != nil {
|
||||
log.Fatalf("finding values for type %v: %v", typeName, err)
|
||||
}
|
||||
analysis.TypesAndValues[typeName] = values
|
||||
|
||||
cammelSnakePairs := make([]CammelSnakePair, len(values))
|
||||
for i, value := range values {
|
||||
cammelSnakePairs[i].CammelRep = value
|
||||
cammelSnakePairs[i].SnakeRep = ToSnake(value)
|
||||
}
|
||||
|
||||
analysis.TypesAndValues[typeName] = cammelSnakePairs
|
||||
|
||||
var buf bytes.Buffer
|
||||
if err := generatedTmpl.Execute(&buf, analysis); err != nil {
|
||||
|
25
template.go
25
template.go
@ -23,20 +23,23 @@ var generatedTmpl = template.Must(template.New("generated").Parse(`
|
||||
package {{.PackageName}}
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
{{range $typename, $values := .TypesAndValues}}
|
||||
|
||||
var (
|
||||
_{{$typename}}NameToValue = map[string]{{$typename}} {
|
||||
{{range $values}}"{{.}}": {{.}},
|
||||
{{range $values}}"{{.SnakeRep}}": {{.CammelRep}},
|
||||
{{end}}
|
||||
}
|
||||
|
||||
_{{$typename}}ValueToName = map[{{$typename}}]string {
|
||||
{{range $values}}{{.}}: "{{.}}",
|
||||
{{range $values}}{{.CammelRep}}: "{{.SnakeRep}}",
|
||||
{{end}}
|
||||
}
|
||||
)
|
||||
@ -45,7 +48,7 @@ func init() {
|
||||
var v {{$typename}}
|
||||
if _, ok := interface{}(v).(fmt.Stringer); ok {
|
||||
_{{$typename}}NameToValue = map[string]{{$typename}} {
|
||||
{{range $values}}interface{}({{.}}).(fmt.Stringer).String(): {{.}},
|
||||
{{range $values}}interface{}({{.CammelRep}}).(fmt.Stringer).String(): {{.CammelRep}},
|
||||
{{end}}
|
||||
}
|
||||
}
|
||||
@ -77,5 +80,21 @@ func (r *{{$typename}}) UnmarshalJSON(data []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
//Scan an input string into this structure for use with GORP
|
||||
func (r *{{$typename}}) Scan(i interface{}) error {
|
||||
switch i.(type) {
|
||||
case string:
|
||||
r.UnmarshalJSON([]byte(i.(string)))
|
||||
default:
|
||||
return errors.Errorf("Can't scan %T into type %T", i, r)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r {{$typename}}) Value() (driver.Value, error) {
|
||||
bytes, err := r.MarshalJSON()
|
||||
return string(bytes), err
|
||||
}
|
||||
|
||||
{{end}}
|
||||
`))
|
||||
|
18
utils.go
Normal file
18
utils.go
Normal file
@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import "unicode"
|
||||
|
||||
func ToSnake(in string) string {
|
||||
runes := []rune(in)
|
||||
length := len(runes)
|
||||
|
||||
var out []rune
|
||||
for i := 0; i < length; i++ {
|
||||
if i > 0 && unicode.IsUpper(runes[i]) && ((i+1 < length && unicode.IsLower(runes[i+1])) || unicode.IsLower(runes[i-1])) {
|
||||
out = append(out, '_')
|
||||
}
|
||||
out = append(out, unicode.ToLower(runes[i]))
|
||||
}
|
||||
|
||||
return string(out)
|
||||
}
|
Loading…
Reference in New Issue
Block a user