2020-08-15 11:41:38 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2020-11-12 18:25:57 +03:00
|
|
|
"go/format"
|
2020-08-15 11:41:38 +03:00
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
)
|
|
|
|
|
|
|
|
type opType struct {
|
2020-12-25 10:06:25 +03:00
|
|
|
Op string
|
|
|
|
Code string
|
|
|
|
HeadToPtrHead func() string
|
|
|
|
HeadToOmitEmptyHead func() string
|
|
|
|
HeadToStringTagHead func() string
|
|
|
|
PtrHeadToHead func() string
|
2020-12-29 18:13:45 +03:00
|
|
|
FieldToEnd func() string
|
2020-12-25 10:06:25 +03:00
|
|
|
FieldToOmitEmptyField func() string
|
|
|
|
FieldToStringTagField func() string
|
2020-08-15 11:41:38 +03:00
|
|
|
}
|
|
|
|
|
2020-12-29 21:50:19 +03:00
|
|
|
func (t opType) IsHeadToPtrHead() bool {
|
|
|
|
return t.Op != t.HeadToPtrHead()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t opType) IsHeadToOmitEmptyHead() bool {
|
|
|
|
return t.Op != t.HeadToOmitEmptyHead()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t opType) IsHeadToStringTagHead() bool {
|
|
|
|
return t.Op != t.HeadToStringTagHead()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t opType) IsPtrHeadToHead() bool {
|
|
|
|
return t.Op != t.PtrHeadToHead()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t opType) IsFieldToEnd() bool {
|
|
|
|
return t.Op != t.FieldToEnd()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t opType) IsFieldToOmitEmptyField() bool {
|
|
|
|
return t.Op != t.FieldToOmitEmptyField()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t opType) IsFieldToStringTagField() bool {
|
|
|
|
return t.Op != t.FieldToStringTagField()
|
|
|
|
}
|
|
|
|
|
2020-12-25 10:06:25 +03:00
|
|
|
func createOpType(op, code string) opType {
|
|
|
|
return opType{
|
|
|
|
Op: op,
|
|
|
|
Code: code,
|
|
|
|
HeadToPtrHead: func() string { return op },
|
|
|
|
HeadToOmitEmptyHead: func() string { return op },
|
|
|
|
HeadToStringTagHead: func() string { return op },
|
|
|
|
PtrHeadToHead: func() string { return op },
|
2020-12-29 18:13:45 +03:00
|
|
|
FieldToEnd: func() string { return op },
|
2020-12-25 10:06:25 +03:00
|
|
|
FieldToOmitEmptyField: func() string { return op },
|
|
|
|
FieldToStringTagField: func() string { return op },
|
|
|
|
}
|
2020-08-15 11:41:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func _main() error {
|
2021-03-15 20:50:19 +03:00
|
|
|
tmpl, err := template.New("").Parse(`// Code generated by internal/cmd/generator. DO NOT EDIT!
|
|
|
|
package encoder
|
2020-08-15 11:41:38 +03:00
|
|
|
|
2021-01-10 14:21:58 +03:00
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2021-03-15 20:50:19 +03:00
|
|
|
type CodeType int
|
2020-08-15 11:41:38 +03:00
|
|
|
|
|
|
|
const (
|
|
|
|
{{- range $index, $type := .CodeTypes }}
|
2021-03-15 20:50:19 +03:00
|
|
|
Code{{ $type }} CodeType = {{ $index }}
|
2020-08-15 11:41:38 +03:00
|
|
|
{{- end }}
|
|
|
|
)
|
|
|
|
|
2021-01-17 16:23:28 +03:00
|
|
|
var opTypeStrings = [{{ .OpLen }}]string{
|
|
|
|
{{- range $type := .OpTypes }}
|
|
|
|
"{{ $type.Op }}",
|
|
|
|
{{- end }}
|
|
|
|
}
|
|
|
|
|
2021-03-15 20:50:19 +03:00
|
|
|
type OpType int
|
2020-08-15 11:41:38 +03:00
|
|
|
|
|
|
|
const (
|
|
|
|
{{- range $index, $type := .OpTypes }}
|
2021-03-15 20:50:19 +03:00
|
|
|
Op{{ $type.Op }} OpType = {{ $index }}
|
2020-08-15 11:41:38 +03:00
|
|
|
{{- end }}
|
|
|
|
)
|
|
|
|
|
2021-03-15 20:50:19 +03:00
|
|
|
func (t OpType) String() string {
|
2021-01-17 16:23:28 +03:00
|
|
|
if int(t) >= {{ .OpLen }} {
|
|
|
|
return ""
|
2020-08-15 11:41:38 +03:00
|
|
|
}
|
2021-01-17 16:23:28 +03:00
|
|
|
return opTypeStrings[int(t)]
|
2020-08-15 11:41:38 +03:00
|
|
|
}
|
|
|
|
|
2021-03-15 20:50:19 +03:00
|
|
|
func (t OpType) CodeType() CodeType {
|
2021-01-10 18:48:20 +03:00
|
|
|
if strings.Contains(t.String(), "Struct") {
|
|
|
|
if strings.Contains(t.String(), "End") {
|
2021-03-15 20:50:19 +03:00
|
|
|
return CodeStructEnd
|
2021-01-10 18:48:20 +03:00
|
|
|
}
|
2021-03-15 20:50:19 +03:00
|
|
|
return CodeStructField
|
2021-01-10 18:48:20 +03:00
|
|
|
}
|
2021-03-15 20:50:19 +03:00
|
|
|
switch t {
|
|
|
|
case OpArray, OpArrayPtr:
|
|
|
|
return CodeArrayHead
|
|
|
|
case OpArrayElem:
|
|
|
|
return CodeArrayElem
|
|
|
|
case OpSlice, OpSlicePtr:
|
|
|
|
return CodeSliceHead
|
|
|
|
case OpSliceElem:
|
|
|
|
return CodeSliceElem
|
|
|
|
case OpMap, OpMapPtr:
|
|
|
|
return CodeMapHead
|
|
|
|
case OpMapKey:
|
|
|
|
return CodeMapKey
|
|
|
|
case OpMapValue:
|
|
|
|
return CodeMapValue
|
|
|
|
case OpMapEnd:
|
|
|
|
return CodeMapEnd
|
2020-08-15 11:41:38 +03:00
|
|
|
}
|
2021-01-10 18:48:20 +03:00
|
|
|
|
2021-03-15 20:50:19 +03:00
|
|
|
return CodeOp
|
2020-08-15 11:41:38 +03:00
|
|
|
}
|
|
|
|
|
2021-03-15 20:50:19 +03:00
|
|
|
func (t OpType) HeadToPtrHead() OpType {
|
2021-01-10 18:48:20 +03:00
|
|
|
if strings.Index(t.String(), "PtrHead") > 0 {
|
|
|
|
return t
|
|
|
|
}
|
2020-12-30 10:13:33 +03:00
|
|
|
|
2021-03-15 20:50:19 +03:00
|
|
|
idx := strings.Index(t.String(), "Head")
|
2021-01-10 18:48:20 +03:00
|
|
|
if idx == -1 {
|
|
|
|
return t
|
|
|
|
}
|
2021-03-15 20:50:19 +03:00
|
|
|
suffix := "Ptr"+t.String()[idx+len("Head"):]
|
2021-01-10 18:48:20 +03:00
|
|
|
|
2021-03-11 13:29:35 +03:00
|
|
|
const toPtrOffset = 3
|
2021-03-15 20:50:19 +03:00
|
|
|
if strings.Contains(OpType(int(t) + toPtrOffset).String(), suffix) {
|
|
|
|
return OpType(int(t) + toPtrOffset)
|
2020-08-15 11:41:38 +03:00
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2021-03-15 20:50:19 +03:00
|
|
|
func (t OpType) HeadToOmitEmptyHead() OpType {
|
2021-03-11 13:29:35 +03:00
|
|
|
const toOmitEmptyOffset = 1
|
2021-03-15 20:50:19 +03:00
|
|
|
if strings.Contains(OpType(int(t) + toOmitEmptyOffset).String(), "OmitEmpty") {
|
|
|
|
return OpType(int(t) + toOmitEmptyOffset)
|
2020-08-15 11:41:38 +03:00
|
|
|
}
|
2021-01-10 18:48:20 +03:00
|
|
|
|
2020-08-15 11:41:38 +03:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2021-03-15 20:50:19 +03:00
|
|
|
func (t OpType) HeadToStringTagHead() OpType {
|
2021-03-11 13:29:35 +03:00
|
|
|
const toStringTagOffset = 2
|
2021-03-15 20:50:19 +03:00
|
|
|
if strings.Contains(OpType(int(t) + toStringTagOffset).String(), "StringTag") {
|
|
|
|
return OpType(int(t) + toStringTagOffset)
|
2020-08-19 13:55:06 +03:00
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2021-03-15 20:50:19 +03:00
|
|
|
func (t OpType) PtrHeadToHead() OpType {
|
2021-01-10 18:48:20 +03:00
|
|
|
idx := strings.Index(t.String(), "Ptr")
|
|
|
|
if idx == -1 {
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
suffix := t.String()[idx+len("Ptr"):]
|
2020-12-30 10:13:33 +03:00
|
|
|
|
2021-03-11 13:29:35 +03:00
|
|
|
const toPtrOffset = 3
|
2021-03-15 20:50:19 +03:00
|
|
|
if strings.Contains(OpType(int(t) - toPtrOffset).String(), suffix) {
|
|
|
|
return OpType(int(t) - toPtrOffset)
|
2020-08-15 11:41:38 +03:00
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2021-03-15 20:50:19 +03:00
|
|
|
func (t OpType) FieldToEnd() OpType {
|
|
|
|
idx := strings.Index(t.String(), "Field")
|
|
|
|
if idx == -1 {
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
suffix := t.String()[idx+len("Field"):]
|
|
|
|
const toEndOffset = 3
|
|
|
|
if strings.Contains(OpType(int(t) + toEndOffset).String(), "End"+suffix) {
|
|
|
|
return OpType(int(t) + toEndOffset)
|
2020-12-29 18:13:45 +03:00
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2021-03-15 20:50:19 +03:00
|
|
|
func (t OpType) FieldToOmitEmptyField() OpType {
|
2021-01-10 18:48:20 +03:00
|
|
|
const toOmitEmptyOffset = 1
|
2021-03-15 20:50:19 +03:00
|
|
|
if strings.Contains(OpType(int(t) + toOmitEmptyOffset).String(), "OmitEmpty") {
|
|
|
|
return OpType(int(t) + toOmitEmptyOffset)
|
2020-08-15 11:41:38 +03:00
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2021-03-15 20:50:19 +03:00
|
|
|
func (t OpType) FieldToStringTagField() OpType {
|
2021-01-10 18:48:20 +03:00
|
|
|
const toStringTagOffset = 2
|
2021-03-15 20:50:19 +03:00
|
|
|
if strings.Contains(OpType(int(t) + toStringTagOffset).String(), "StringTag") {
|
|
|
|
return OpType(int(t) + toStringTagOffset)
|
2020-08-19 13:55:06 +03:00
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2020-08-15 11:41:38 +03:00
|
|
|
`)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
codeTypes := []string{
|
|
|
|
"Op",
|
|
|
|
"ArrayHead",
|
|
|
|
"ArrayElem",
|
|
|
|
"SliceHead",
|
|
|
|
"SliceElem",
|
|
|
|
"MapHead",
|
|
|
|
"MapKey",
|
|
|
|
"MapValue",
|
2020-09-16 08:51:37 +03:00
|
|
|
"MapEnd",
|
2020-08-15 11:41:38 +03:00
|
|
|
"StructFieldRecursive",
|
|
|
|
"StructField",
|
2020-12-29 18:13:45 +03:00
|
|
|
"StructEnd",
|
2020-08-15 11:41:38 +03:00
|
|
|
}
|
|
|
|
primitiveTypes := []string{
|
2021-03-11 13:29:35 +03:00
|
|
|
"int", "uint", "float32", "float64", "bool", "string", "bytes", "number",
|
|
|
|
"array", "map", "slice", "struct", "MarshalJSON", "MarshalText", "recursive",
|
2021-02-22 09:16:53 +03:00
|
|
|
"intString", "uintString",
|
2021-03-11 13:29:35 +03:00
|
|
|
"intPtr", "uintPtr", "float32Ptr", "float64Ptr", "boolPtr", "stringPtr", "bytesPtr", "numberPtr",
|
|
|
|
"arrayPtr", "mapPtr", "slicePtr", "marshalJSONPtr", "marshalTextPtr", "interfacePtr", "recursivePtr",
|
2020-08-15 11:41:38 +03:00
|
|
|
}
|
|
|
|
primitiveTypesUpper := []string{}
|
|
|
|
for _, typ := range primitiveTypes {
|
|
|
|
primitiveTypesUpper = append(primitiveTypesUpper, strings.ToUpper(string(typ[0]))+typ[1:])
|
|
|
|
}
|
|
|
|
opTypes := []opType{
|
2020-12-25 10:06:25 +03:00
|
|
|
createOpType("End", "Op"),
|
|
|
|
createOpType("Interface", "Op"),
|
|
|
|
createOpType("Ptr", "Op"),
|
|
|
|
createOpType("SliceElem", "SliceElem"),
|
|
|
|
createOpType("SliceEnd", "Op"),
|
|
|
|
createOpType("ArrayElem", "ArrayElem"),
|
|
|
|
createOpType("ArrayEnd", "Op"),
|
|
|
|
createOpType("MapKey", "MapKey"),
|
|
|
|
createOpType("MapValue", "MapValue"),
|
|
|
|
createOpType("MapEnd", "Op"),
|
|
|
|
createOpType("StructFieldRecursiveEnd", "Op"),
|
2020-12-29 18:13:45 +03:00
|
|
|
createOpType("StructAnonymousEnd", "StructEnd"),
|
2020-08-15 11:41:38 +03:00
|
|
|
}
|
|
|
|
for _, typ := range primitiveTypesUpper {
|
2020-12-25 10:06:25 +03:00
|
|
|
typ := typ
|
2021-01-11 13:21:30 +03:00
|
|
|
opTypes = append(opTypes, createOpType(typ, "Op"))
|
2020-08-15 11:41:38 +03:00
|
|
|
}
|
2021-01-10 14:21:58 +03:00
|
|
|
for _, typ := range append(primitiveTypesUpper, "") {
|
2021-03-11 13:29:35 +03:00
|
|
|
for _, ptrOrNot := range []string{"", "Ptr"} {
|
|
|
|
for _, opt := range []string{"", "OmitEmpty", "StringTag"} {
|
|
|
|
ptrOrNot := ptrOrNot
|
|
|
|
opt := opt
|
|
|
|
typ := typ
|
|
|
|
|
|
|
|
op := fmt.Sprintf(
|
2021-03-15 20:50:19 +03:00
|
|
|
"Struct%sHead%s%s",
|
2021-03-11 13:29:35 +03:00
|
|
|
ptrOrNot,
|
|
|
|
opt,
|
|
|
|
typ,
|
|
|
|
)
|
|
|
|
opTypes = append(opTypes, opType{
|
|
|
|
Op: op,
|
|
|
|
Code: "StructField",
|
|
|
|
HeadToPtrHead: func() string {
|
|
|
|
return fmt.Sprintf(
|
2021-03-15 20:50:19 +03:00
|
|
|
"StructPtrHead%s%s",
|
2021-03-11 13:29:35 +03:00
|
|
|
opt,
|
|
|
|
typ,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
HeadToOmitEmptyHead: func() string {
|
|
|
|
return fmt.Sprintf(
|
2021-03-15 20:50:19 +03:00
|
|
|
"Struct%sHeadOmitEmpty%s",
|
2021-01-11 13:21:30 +03:00
|
|
|
ptrOrNot,
|
2021-03-11 13:29:35 +03:00
|
|
|
typ,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
HeadToStringTagHead: func() string {
|
|
|
|
return fmt.Sprintf(
|
2021-03-15 20:50:19 +03:00
|
|
|
"Struct%sHeadStringTag%s",
|
2021-03-11 13:29:35 +03:00
|
|
|
ptrOrNot,
|
|
|
|
typ,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
PtrHeadToHead: func() string {
|
|
|
|
return fmt.Sprintf(
|
2021-03-15 20:50:19 +03:00
|
|
|
"StructHead%s%s",
|
2021-01-11 13:21:30 +03:00
|
|
|
opt,
|
|
|
|
typ,
|
|
|
|
)
|
2021-03-11 13:29:35 +03:00
|
|
|
},
|
|
|
|
FieldToEnd: func() string { return op },
|
|
|
|
FieldToOmitEmptyField: func() string { return op },
|
|
|
|
FieldToStringTagField: func() string { return op },
|
|
|
|
})
|
2021-01-08 20:28:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-10 14:21:58 +03:00
|
|
|
for _, typ := range append(primitiveTypesUpper, "") {
|
2021-01-11 13:21:30 +03:00
|
|
|
for _, opt := range []string{"", "OmitEmpty", "StringTag"} {
|
|
|
|
opt := opt
|
|
|
|
typ := typ
|
|
|
|
|
|
|
|
op := fmt.Sprintf(
|
|
|
|
"StructField%s%s",
|
|
|
|
opt,
|
|
|
|
typ,
|
|
|
|
)
|
|
|
|
opTypes = append(opTypes, opType{
|
|
|
|
Op: op,
|
|
|
|
Code: "StructField",
|
|
|
|
HeadToPtrHead: func() string { return op },
|
|
|
|
HeadToOmitEmptyHead: func() string { return op },
|
|
|
|
HeadToStringTagHead: func() string { return op },
|
|
|
|
PtrHeadToHead: func() string { return op },
|
|
|
|
FieldToEnd: func() string {
|
|
|
|
switch typ {
|
2021-03-11 13:29:35 +03:00
|
|
|
case "", "Array", "Map", "Slice", "Struct", "Recursive":
|
2021-01-11 13:21:30 +03:00
|
|
|
return op
|
|
|
|
}
|
|
|
|
return fmt.Sprintf(
|
|
|
|
"StructEnd%s%s",
|
|
|
|
opt,
|
|
|
|
typ,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
FieldToOmitEmptyField: func() string {
|
|
|
|
return fmt.Sprintf(
|
|
|
|
"StructFieldOmitEmpty%s",
|
|
|
|
typ,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
FieldToStringTagField: func() string {
|
|
|
|
return fmt.Sprintf(
|
|
|
|
"StructFieldStringTag%s",
|
|
|
|
typ,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
})
|
2020-12-25 10:06:25 +03:00
|
|
|
}
|
2021-01-11 13:21:30 +03:00
|
|
|
for _, opt := range []string{"", "OmitEmpty", "StringTag"} {
|
|
|
|
opt := opt
|
|
|
|
typ := typ
|
|
|
|
|
|
|
|
op := fmt.Sprintf(
|
|
|
|
"StructEnd%s%s",
|
|
|
|
opt,
|
|
|
|
typ,
|
|
|
|
)
|
|
|
|
opTypes = append(opTypes, opType{
|
|
|
|
Op: op,
|
|
|
|
Code: "StructEnd",
|
|
|
|
HeadToPtrHead: func() string { return op },
|
|
|
|
HeadToOmitEmptyHead: func() string { return op },
|
|
|
|
HeadToStringTagHead: func() string { return op },
|
|
|
|
PtrHeadToHead: func() string { return op },
|
|
|
|
FieldToEnd: func() string { return op },
|
|
|
|
FieldToOmitEmptyField: func() string { return op },
|
|
|
|
FieldToStringTagField: func() string { return op },
|
|
|
|
})
|
2020-12-29 17:17:15 +03:00
|
|
|
}
|
|
|
|
}
|
2020-08-15 11:41:38 +03:00
|
|
|
var b bytes.Buffer
|
|
|
|
if err := tmpl.Execute(&b, struct {
|
2021-01-11 13:05:06 +03:00
|
|
|
CodeTypes []string
|
|
|
|
OpTypes []opType
|
|
|
|
OpLen int
|
2020-08-15 11:41:38 +03:00
|
|
|
}{
|
2021-01-11 13:05:06 +03:00
|
|
|
CodeTypes: codeTypes,
|
|
|
|
OpTypes: opTypes,
|
|
|
|
OpLen: len(opTypes),
|
2020-08-15 11:41:38 +03:00
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-15 20:50:19 +03:00
|
|
|
path := filepath.Join(repoRoot(), "internal", "encoder", "optype.go")
|
2020-11-12 18:25:57 +03:00
|
|
|
buf, err := format.Source(b.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ioutil.WriteFile(path, buf, 0644)
|
2020-08-15 11:41:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func repoRoot() string {
|
|
|
|
_, file, _, _ := runtime.Caller(0)
|
2021-03-15 20:50:19 +03:00
|
|
|
relativePathFromRepoRoot := filepath.Join("internal", "cmd", "generator")
|
2020-08-15 11:41:38 +03:00
|
|
|
return strings.TrimSuffix(filepath.Dir(file), relativePathFromRepoRoot)
|
|
|
|
}
|
|
|
|
|
2021-03-15 20:50:19 +03:00
|
|
|
//go:generate go run main.go
|
2020-08-15 11:41:38 +03:00
|
|
|
func main() {
|
|
|
|
if err := _main(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|