Reduce code size of headToOnlyHead

This commit is contained in:
Masaaki Goshima 2021-01-10 20:21:58 +09:00
parent 14cde782af
commit b7f79b13eb
3 changed files with 76193 additions and 70222 deletions

View File

@ -95,6 +95,10 @@ func _main() error {
tmpl, err := template.New("").Parse(`// Code generated by cmd/generator. DO NOT EDIT! tmpl, err := template.New("").Parse(`// Code generated by cmd/generator. DO NOT EDIT!
package json package json
import (
"strings"
)
type codeType int type codeType int
const ( const (
@ -254,13 +258,13 @@ func (t opType) headToOnlyHead() opType {
return opType(int(t.toNotIndent().headToOnlyHead()) + {{ .OpLen }}) return opType(int(t.toNotIndent().headToOnlyHead()) + {{ .OpLen }})
} }
switch t { if strings.HasSuffix(t.String(), "Head") || strings.HasSuffix(t.String(), "HeadOmitEmpty") || strings.HasSuffix(t.String(), "HeadStringTag") {
{{- range $type := .OpNotIndentTypes }} return t
{{- if $type.IsHeadToOnlyHead }} }
case op{{ $type.Op }}:
return op{{ call $type.HeadToOnlyHead }} const toOnlyOffset = 1
{{- end }} if opType(int(t) + toOnlyOffset).String() == t.String() + "Only" {
{{- end }} return opType(int(t) + toOnlyOffset)
} }
return t return t
} }
@ -398,247 +402,158 @@ func (t opType) fieldToStringTagField() opType {
} }
opTypes = append(opTypes, optype) opTypes = append(opTypes, optype)
} }
for _, escapedOrNot := range []string{"", "Escaped"} { for _, typ := range append(primitiveTypesUpper, "") {
for _, ptrOrNot := range []string{"", "Ptr", "NPtr"} { for _, escapedOrNot := range []string{"", "Escaped"} {
for _, headType := range []string{"", "Anonymous"} { for _, ptrOrNot := range []string{"", "Ptr", "NPtr"} {
for _, opt := range []string{"", "OmitEmpty", "StringTag"} { for _, headType := range []string{"", "Anonymous"} {
for _, typ := range append(primitiveTypesUpper, "") { for _, opt := range []string{"", "OmitEmpty", "StringTag"} {
escapedOrNot := escapedOrNot for _, onlyOrNot := range []string{"", "Only"} {
ptrOrNot := ptrOrNot escapedOrNot := escapedOrNot
headType := headType ptrOrNot := ptrOrNot
opt := opt headType := headType
typ := typ opt := opt
typ := typ
onlyOrNot := onlyOrNot
op := fmt.Sprintf( if onlyOrNot == "Only" {
"Struct%sField%s%sHead%s%s", /*
escapedOrNot, if typ == "" && ptrOrNot == "Ptr" {
ptrOrNot, continue
headType, }
opt, switch typ {
typ, case "Array", "Map", "MapLoad", "Slice",
) "Struct", "Recursive", "MarshalJSON", "MarshalText",
opTypes = append(opTypes, opType{ "IntString", "Int8String", "Int16String", "Int32String", "Int64String",
Op: op, "UintString", "Uint8String", "Uint16String", "Uint32String", "Uint64String":
Code: "StructField", continue
Indent: func() string { return fmt.Sprintf("%sIndent", op) }, }
Escaped: func() string { */
switch typ { }
case "String", "StringPtr", "StringNPtr":
op := fmt.Sprintf(
"Struct%sField%s%sHead%s%s%s",
escapedOrNot,
ptrOrNot,
headType,
opt,
typ,
onlyOrNot,
)
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%s%s",
ptrOrNot,
headType,
opt,
typ,
onlyOrNot,
)
}
return fmt.Sprintf( return fmt.Sprintf(
"StructEscapedField%s%sHead%sEscaped%s", "StructEscapedField%s%sHead%s%s%s",
ptrOrNot,
headType,
opt,
typ,
onlyOrNot,
)
},
HeadToPtrHead: func() string {
return fmt.Sprintf(
"Struct%sFieldPtr%sHead%s%s%s",
escapedOrNot,
headType,
opt,
typ,
onlyOrNot,
)
},
HeadToNPtrHead: func() string {
return fmt.Sprintf(
"Struct%sFieldNPtr%sHead%s%s%s",
escapedOrNot,
headType,
opt,
typ,
onlyOrNot,
)
},
HeadToAnonymousHead: func() string {
return fmt.Sprintf(
"Struct%sField%sAnonymousHead%s%s%s",
escapedOrNot,
ptrOrNot,
opt,
typ,
onlyOrNot,
)
},
HeadToOmitEmptyHead: func() string {
return fmt.Sprintf(
"Struct%sField%s%sHeadOmitEmpty%s%s",
escapedOrNot,
ptrOrNot,
headType,
typ,
onlyOrNot,
)
},
HeadToStringTagHead: func() string {
return fmt.Sprintf(
"Struct%sField%s%sHeadStringTag%s%s",
escapedOrNot,
ptrOrNot,
headType,
typ,
onlyOrNot,
)
},
HeadToOnlyHead: func() string {
switch typ {
case "", "Array", "Map", "MapLoad", "Slice",
"Struct", "Recursive", "MarshalJSON", "MarshalText",
"IntString", "Int8String", "Int16String", "Int32String", "Int64String",
"UintString", "Uint8String", "Uint16String", "Uint32String", "Uint64String":
return op
}
return fmt.Sprintf(
"Struct%sField%s%sHead%s%sOnly",
escapedOrNot,
ptrOrNot, ptrOrNot,
headType, headType,
opt, opt,
typ, typ,
) )
} },
return fmt.Sprintf( PtrHeadToHead: func() string {
"StructEscapedField%s%sHead%s%s",
ptrOrNot,
headType,
opt,
typ,
)
},
HeadToPtrHead: func() string {
return fmt.Sprintf(
"Struct%sFieldPtr%sHead%s%s",
escapedOrNot,
headType,
opt,
typ,
)
},
HeadToNPtrHead: func() string {
return fmt.Sprintf(
"Struct%sFieldNPtr%sHead%s%s",
escapedOrNot,
headType,
opt,
typ,
)
},
HeadToAnonymousHead: func() string {
return fmt.Sprintf(
"Struct%sField%sAnonymousHead%s%s",
escapedOrNot,
ptrOrNot,
opt,
typ,
)
},
HeadToOmitEmptyHead: func() string {
return fmt.Sprintf(
"Struct%sField%s%sHeadOmitEmpty%s",
escapedOrNot,
ptrOrNot,
headType,
typ,
)
},
HeadToStringTagHead: func() string {
return fmt.Sprintf(
"Struct%sField%s%sHeadStringTag%s",
escapedOrNot,
ptrOrNot,
headType,
typ,
)
},
HeadToOnlyHead: func() string {
switch typ {
case "", "Array", "Map", "MapLoad", "Slice",
"Struct", "Recursive", "MarshalJSON", "MarshalText",
"IntString", "Int8String", "Int16String", "Int32String", "Int64String",
"UintString", "Uint8String", "Uint16String", "Uint32String", "Uint64String":
return op
}
return fmt.Sprintf(
"Struct%sField%s%sHead%s%sOnly",
escapedOrNot,
ptrOrNot,
headType,
opt,
typ,
)
},
PtrHeadToHead: func() string {
return fmt.Sprintf(
"Struct%sField%sHead%s%s",
escapedOrNot,
headType,
opt,
typ,
)
},
FieldToEnd: func() string { return op },
FieldToOmitEmptyField: func() string { return op },
FieldToStringTagField: func() string { return op },
})
}
}
}
}
}
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( return fmt.Sprintf(
"StructEscapedField%s%sHead%sEscaped%sOnly", "Struct%sField%sHead%s%s%s",
ptrOrNot, escapedOrNot,
headType, headType,
opt, opt,
typ, typ,
onlyOrNot,
) )
} },
return fmt.Sprintf( FieldToEnd: func() string { return op },
"StructEscapedField%s%sHead%s%sOnly", FieldToOmitEmptyField: func() string { return op },
ptrOrNot, FieldToStringTagField: func() string { return op },
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 _, typ := range append(primitiveTypesUpper, "") {
for _, opt := range []string{"", "OmitEmpty", "StringTag"} { for _, escapedOrNot := range []string{"", "Escaped"} {
for _, typ := range append(primitiveTypesUpper, "") { for _, opt := range []string{"", "OmitEmpty", "StringTag"} {
escapedOrNot := escapedOrNot escapedOrNot := escapedOrNot
opt := opt opt := opt
typ := typ typ := typ
@ -705,9 +620,9 @@ func (t opType) fieldToStringTagField() opType {
} }
} }
} }
for _, escapedOrNot := range []string{"", "Escaped"} { for _, typ := range append(primitiveTypesUpper, "") {
for _, opt := range []string{"", "OmitEmpty", "StringTag"} { for _, escapedOrNot := range []string{"", "Escaped"} {
for _, typ := range append(primitiveTypesUpper, "") { for _, opt := range []string{"", "OmitEmpty", "StringTag"} {
escapedOrNot := escapedOrNot escapedOrNot := escapedOrNot
opt := opt opt := opt
typ := typ typ := typ

View File

@ -1,5 +1,3 @@
// +build ignore
package json package json
import ( import (

File diff suppressed because it is too large Load Diff