mirror of https://github.com/goccy/go-json.git
Reduce code size of headToOnlyHead
This commit is contained in:
parent
14cde782af
commit
b7f79b13eb
|
@ -95,6 +95,10 @@ func _main() error {
|
|||
tmpl, err := template.New("").Parse(`// Code generated by cmd/generator. DO NOT EDIT!
|
||||
package json
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type codeType int
|
||||
|
||||
const (
|
||||
|
@ -254,13 +258,13 @@ func (t opType) headToOnlyHead() opType {
|
|||
return opType(int(t.toNotIndent().headToOnlyHead()) + {{ .OpLen }})
|
||||
}
|
||||
|
||||
switch t {
|
||||
{{- range $type := .OpNotIndentTypes }}
|
||||
{{- if $type.IsHeadToOnlyHead }}
|
||||
case op{{ $type.Op }}:
|
||||
return op{{ call $type.HeadToOnlyHead }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
if strings.HasSuffix(t.String(), "Head") || strings.HasSuffix(t.String(), "HeadOmitEmpty") || strings.HasSuffix(t.String(), "HeadStringTag") {
|
||||
return t
|
||||
}
|
||||
|
||||
const toOnlyOffset = 1
|
||||
if opType(int(t) + toOnlyOffset).String() == t.String() + "Only" {
|
||||
return opType(int(t) + toOnlyOffset)
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
@ -398,24 +402,42 @@ func (t opType) fieldToStringTagField() opType {
|
|||
}
|
||||
opTypes = append(opTypes, optype)
|
||||
}
|
||||
for _, typ := range append(primitiveTypesUpper, "") {
|
||||
for _, escapedOrNot := range []string{"", "Escaped"} {
|
||||
for _, ptrOrNot := range []string{"", "Ptr", "NPtr"} {
|
||||
for _, headType := range []string{"", "Anonymous"} {
|
||||
for _, opt := range []string{"", "OmitEmpty", "StringTag"} {
|
||||
for _, typ := range append(primitiveTypesUpper, "") {
|
||||
for _, onlyOrNot := range []string{"", "Only"} {
|
||||
escapedOrNot := escapedOrNot
|
||||
ptrOrNot := ptrOrNot
|
||||
headType := headType
|
||||
opt := opt
|
||||
typ := typ
|
||||
onlyOrNot := onlyOrNot
|
||||
|
||||
if onlyOrNot == "Only" {
|
||||
/*
|
||||
if typ == "" && ptrOrNot == "Ptr" {
|
||||
continue
|
||||
}
|
||||
switch typ {
|
||||
case "Array", "Map", "MapLoad", "Slice",
|
||||
"Struct", "Recursive", "MarshalJSON", "MarshalText",
|
||||
"IntString", "Int8String", "Int16String", "Int32String", "Int64String",
|
||||
"UintString", "Uint8String", "Uint16String", "Uint32String", "Uint64String":
|
||||
continue
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
op := fmt.Sprintf(
|
||||
"Struct%sField%s%sHead%s%s",
|
||||
"Struct%sField%s%sHead%s%s%s",
|
||||
escapedOrNot,
|
||||
ptrOrNot,
|
||||
headType,
|
||||
opt,
|
||||
typ,
|
||||
onlyOrNot,
|
||||
)
|
||||
opTypes = append(opTypes, opType{
|
||||
Op: op,
|
||||
|
@ -425,64 +447,71 @@ func (t opType) fieldToStringTagField() opType {
|
|||
switch typ {
|
||||
case "String", "StringPtr", "StringNPtr":
|
||||
return fmt.Sprintf(
|
||||
"StructEscapedField%s%sHead%sEscaped%s",
|
||||
"StructEscapedField%s%sHead%sEscaped%s%s",
|
||||
ptrOrNot,
|
||||
headType,
|
||||
opt,
|
||||
typ,
|
||||
onlyOrNot,
|
||||
)
|
||||
}
|
||||
return fmt.Sprintf(
|
||||
"StructEscapedField%s%sHead%s%s",
|
||||
"StructEscapedField%s%sHead%s%s%s",
|
||||
ptrOrNot,
|
||||
headType,
|
||||
opt,
|
||||
typ,
|
||||
onlyOrNot,
|
||||
)
|
||||
},
|
||||
HeadToPtrHead: func() string {
|
||||
return fmt.Sprintf(
|
||||
"Struct%sFieldPtr%sHead%s%s",
|
||||
"Struct%sFieldPtr%sHead%s%s%s",
|
||||
escapedOrNot,
|
||||
headType,
|
||||
opt,
|
||||
typ,
|
||||
onlyOrNot,
|
||||
)
|
||||
},
|
||||
HeadToNPtrHead: func() string {
|
||||
return fmt.Sprintf(
|
||||
"Struct%sFieldNPtr%sHead%s%s",
|
||||
"Struct%sFieldNPtr%sHead%s%s%s",
|
||||
escapedOrNot,
|
||||
headType,
|
||||
opt,
|
||||
typ,
|
||||
onlyOrNot,
|
||||
)
|
||||
},
|
||||
HeadToAnonymousHead: func() string {
|
||||
return fmt.Sprintf(
|
||||
"Struct%sField%sAnonymousHead%s%s",
|
||||
"Struct%sField%sAnonymousHead%s%s%s",
|
||||
escapedOrNot,
|
||||
ptrOrNot,
|
||||
opt,
|
||||
typ,
|
||||
onlyOrNot,
|
||||
)
|
||||
},
|
||||
HeadToOmitEmptyHead: func() string {
|
||||
return fmt.Sprintf(
|
||||
"Struct%sField%s%sHeadOmitEmpty%s",
|
||||
"Struct%sField%s%sHeadOmitEmpty%s%s",
|
||||
escapedOrNot,
|
||||
ptrOrNot,
|
||||
headType,
|
||||
typ,
|
||||
onlyOrNot,
|
||||
)
|
||||
},
|
||||
HeadToStringTagHead: func() string {
|
||||
return fmt.Sprintf(
|
||||
"Struct%sField%s%sHeadStringTag%s",
|
||||
"Struct%sField%s%sHeadStringTag%s%s",
|
||||
escapedOrNot,
|
||||
ptrOrNot,
|
||||
headType,
|
||||
typ,
|
||||
onlyOrNot,
|
||||
)
|
||||
},
|
||||
HeadToOnlyHead: func() string {
|
||||
|
@ -504,11 +533,12 @@ func (t opType) fieldToStringTagField() opType {
|
|||
},
|
||||
PtrHeadToHead: func() string {
|
||||
return fmt.Sprintf(
|
||||
"Struct%sField%sHead%s%s",
|
||||
"Struct%sField%sHead%s%s%s",
|
||||
escapedOrNot,
|
||||
headType,
|
||||
opt,
|
||||
typ,
|
||||
onlyOrNot,
|
||||
)
|
||||
},
|
||||
FieldToEnd: func() string { return op },
|
||||
|
@ -520,125 +550,10 @@ func (t opType) fieldToStringTagField() opType {
|
|||
}
|
||||
}
|
||||
}
|
||||
for _, escapedOrNot := range []string{"", "Escaped"} {
|
||||
for _, ptrOrNot := range []string{"", "Ptr", "NPtr"} {
|
||||
for _, headType := range []string{"", "Anonymous"} {
|
||||
for _, opt := range []string{"", "OmitEmpty", "StringTag"} {
|
||||
for _, typ := range []string{
|
||||
"", "Int", "Int8", "Int16", "Int32", "Int64",
|
||||
"Uint", "Uint8", "Uint16", "Uint32", "Uint64",
|
||||
"Float32", "Float64", "Bool", "String", "EscapedString", "Bytes",
|
||||
"IntPtr", "Int8Ptr", "Int16Ptr", "Int32Ptr", "Int64Ptr",
|
||||
"UintPtr", "Uint8Ptr", "Uint16Ptr", "Uint32Ptr", "Uint64Ptr",
|
||||
"Float32Ptr", "Float64Ptr", "BoolPtr", "StringPtr", "EscapedStringPtr", "BytesPtr",
|
||||
"IntNPtr", "Int8NPtr", "Int16NPtr", "Int32NPtr", "Int64NPtr",
|
||||
"UintNPtr", "Uint8NPtr", "Uint16NPtr", "Uint32NPtr", "Uint64NPtr",
|
||||
"Float32NPtr", "Float64NPtr", "BoolNPtr", "StringNPtr", "EscapedStringNPtr", "BytesNPtr",
|
||||
} {
|
||||
escapedOrNot := escapedOrNot
|
||||
ptrOrNot := ptrOrNot
|
||||
headType := headType
|
||||
opt := opt
|
||||
typ := typ
|
||||
|
||||
op := fmt.Sprintf(
|
||||
"Struct%sField%s%sHead%s%sOnly",
|
||||
escapedOrNot,
|
||||
ptrOrNot,
|
||||
headType,
|
||||
opt,
|
||||
typ,
|
||||
)
|
||||
opTypes = append(opTypes, opType{
|
||||
Op: op,
|
||||
Code: "StructField",
|
||||
Indent: func() string { return fmt.Sprintf("%sIndent", op) },
|
||||
Escaped: func() string {
|
||||
switch typ {
|
||||
case "String", "StringPtr", "StringNPtr":
|
||||
return fmt.Sprintf(
|
||||
"StructEscapedField%s%sHead%sEscaped%sOnly",
|
||||
ptrOrNot,
|
||||
headType,
|
||||
opt,
|
||||
typ,
|
||||
)
|
||||
}
|
||||
return fmt.Sprintf(
|
||||
"StructEscapedField%s%sHead%s%sOnly",
|
||||
ptrOrNot,
|
||||
headType,
|
||||
opt,
|
||||
typ,
|
||||
)
|
||||
},
|
||||
HeadToPtrHead: func() string {
|
||||
return fmt.Sprintf(
|
||||
"Struct%sFieldPtr%sHead%s%sOnly",
|
||||
escapedOrNot,
|
||||
headType,
|
||||
opt,
|
||||
typ,
|
||||
)
|
||||
},
|
||||
HeadToNPtrHead: func() string {
|
||||
return fmt.Sprintf(
|
||||
"Struct%sFieldNPtr%sHead%s%sOnly",
|
||||
escapedOrNot,
|
||||
headType,
|
||||
opt,
|
||||
typ,
|
||||
)
|
||||
},
|
||||
HeadToAnonymousHead: func() string {
|
||||
return fmt.Sprintf(
|
||||
"Struct%sField%sAnonymousHead%s%sOnly",
|
||||
escapedOrNot,
|
||||
ptrOrNot,
|
||||
opt,
|
||||
typ,
|
||||
)
|
||||
},
|
||||
HeadToOmitEmptyHead: func() string {
|
||||
return fmt.Sprintf(
|
||||
"Struct%sField%s%sHeadOmitEmpty%sOnly",
|
||||
escapedOrNot,
|
||||
ptrOrNot,
|
||||
headType,
|
||||
typ,
|
||||
)
|
||||
},
|
||||
HeadToStringTagHead: func() string {
|
||||
return fmt.Sprintf(
|
||||
"Struct%sField%s%sHeadStringTag%sOnly",
|
||||
escapedOrNot,
|
||||
ptrOrNot,
|
||||
headType,
|
||||
typ,
|
||||
)
|
||||
},
|
||||
PtrHeadToHead: func() string {
|
||||
return fmt.Sprintf(
|
||||
"Struct%sField%sHead%s%sOnly",
|
||||
escapedOrNot,
|
||||
headType,
|
||||
opt,
|
||||
typ,
|
||||
)
|
||||
},
|
||||
HeadToOnlyHead: func() string { return op },
|
||||
FieldToEnd: func() string { return op },
|
||||
FieldToOmitEmptyField: func() string { return op },
|
||||
FieldToStringTagField: func() string { return op },
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, escapedOrNot := range []string{"", "Escaped"} {
|
||||
for _, opt := range []string{"", "OmitEmpty", "StringTag"} {
|
||||
for _, typ := range append(primitiveTypesUpper, "") {
|
||||
for _, escapedOrNot := range []string{"", "Escaped"} {
|
||||
for _, opt := range []string{"", "OmitEmpty", "StringTag"} {
|
||||
escapedOrNot := escapedOrNot
|
||||
opt := opt
|
||||
typ := typ
|
||||
|
@ -705,9 +620,9 @@ func (t opType) fieldToStringTagField() opType {
|
|||
}
|
||||
}
|
||||
}
|
||||
for _, typ := range append(primitiveTypesUpper, "") {
|
||||
for _, escapedOrNot := range []string{"", "Escaped"} {
|
||||
for _, opt := range []string{"", "OmitEmpty", "StringTag"} {
|
||||
for _, typ := range append(primitiveTypesUpper, "") {
|
||||
escapedOrNot := escapedOrNot
|
||||
opt := opt
|
||||
typ := typ
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
// +build ignore
|
||||
|
||||
package json
|
||||
|
||||
import (
|
||||
|
|
146030
encode_optype.go
146030
encode_optype.go
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue