diff --git a/generator/golang/templates/client.go b/generator/golang/templates/client.go.tmpl similarity index 78% rename from generator/golang/templates/client.go rename to generator/golang/templates/client.go.tmpl index 4f257645..18cbb20d 100644 --- a/generator/golang/templates/client.go +++ b/generator/golang/templates/client.go.tmpl @@ -1,21 +1,3 @@ -// Copyright 2022 CloudWeGo Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package templates - -// Client . -var Client = ` {{define "Client"}} {{- UseStdLibrary "thrift"}} {{- $BasePrefix := ServicePrefix .Base}} @@ -106,4 +88,3 @@ func (p *{{$ClientName}}) {{- template "FunctionSignature" . -}} { } {{- end}}{{/* range .Functions */}} {{- end}}{{/* define "Cleint" */}} -` diff --git a/generator/golang/templates/constant.go b/generator/golang/templates/constant.go.tmpl similarity index 52% rename from generator/golang/templates/constant.go rename to generator/golang/templates/constant.go.tmpl index 5e8e6432..8c4f2ee4 100644 --- a/generator/golang/templates/constant.go +++ b/generator/golang/templates/constant.go.tmpl @@ -1,21 +1,3 @@ -// Copyright 2021 CloudWeGo Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package templates - -// Constant . -var Constant = ` {{define "Constant"}} {{- $Consts := .Constants.GoConstants}} {{- if $Consts}} @@ -41,4 +23,3 @@ var ( ) {{- end}} {{end}}{{- /* define "Constant" */ -}} -` diff --git a/generator/golang/templates/deep_equal.go b/generator/golang/templates/deep_equal.go.tmpl similarity index 72% rename from generator/golang/templates/deep_equal.go rename to generator/golang/templates/deep_equal.go.tmpl index 40fdda5f..13e36a4f 100644 --- a/generator/golang/templates/deep_equal.go +++ b/generator/golang/templates/deep_equal.go.tmpl @@ -1,21 +1,3 @@ -// Copyright 2021 CloudWeGo Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package templates - -// StructLikeDeepEqual . -var StructLikeDeepEqual = ` {{define "StructLikeDeepEqual"}} {{- $TypeName := .GoName}} func (p *{{$TypeName}}) DeepEqual(ano *{{$TypeName}}) bool { @@ -32,10 +14,7 @@ func (p *{{$TypeName}}) DeepEqual(ano *{{$TypeName}}) bool { return true } {{- end}}{{/* "StructLikeDeepEqual" */}} -` -// StructLikeDeepEqualField . -var StructLikeDeepEqualField = ` {{define "StructLikeDeepEqualField"}} {{- $TypeName := .GoName}} {{- range .Fields}} @@ -46,10 +25,7 @@ func (p *{{$TypeName}}) {{.DeepEqual}}({{$ctx.Source}} {{$ctx.TypeName}}) bool { } {{- end}}{{/* range .Fields */}} {{- end}}{{/* "StructLikeDeepEqualField" */}} -` -// FieldDeepEqual . -var FieldDeepEqual = ` {{define "FieldDeepEqual"}} {{- if .Type.Category.IsStructLike}} {{- template "FieldDeepEqualStructLike" .}} @@ -59,19 +35,13 @@ var FieldDeepEqual = ` {{- template "FieldDeepEqualBase" .}} {{- end}} {{- end}}{{/* "FieldDeepEqual" */}} -` -// FieldDeepEqualStructLike . -var FieldDeepEqualStructLike = ` {{define "FieldDeepEqualStructLike"}} if !{{.Target}}.DeepEqual({{.Source}}) { return false } {{- end}}{{/* "FieldDeepEqualStructLike" */}} -` -// FieldDeepEqualBase . -var FieldDeepEqualBase = ` {{define "FieldDeepEqualBase"}} {{- if .IsPointer}} if {{.Target}} == {{.Source}} { @@ -99,10 +69,7 @@ var FieldDeepEqualBase = ` } {{- end}}{{/* if .Type.Category.IsString */}} {{- end}}{{/* "FieldDeepEqualBase" */}} -` -// FieldDeepEqualContainer . -var FieldDeepEqualContainer = ` {{define "FieldDeepEqualContainer"}} {{- if .IsPointer}} if {{.Target}} == {{.Source}} { @@ -123,4 +90,3 @@ var FieldDeepEqualContainer = ` {{- template "FieldDeepEqual" $ctx}} } {{- end}}{{/* "FieldDeepEqualContainer" */}} -` diff --git a/generator/golang/templates/enum.go b/generator/golang/templates/enum.go.tmpl similarity index 71% rename from generator/golang/templates/enum.go rename to generator/golang/templates/enum.go.tmpl index af095765..0216b08c 100644 --- a/generator/golang/templates/enum.go +++ b/generator/golang/templates/enum.go.tmpl @@ -1,21 +1,3 @@ -// Copyright 2021 CloudWeGo Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package templates - -// Enum . -var Enum = ` {{define "Enum"}} {{- $EnumType := .GoName}} {{InsertionPoint "enum" .Name}} @@ -86,4 +68,3 @@ func (p *{{$EnumType}}) Value() (driver.Value, error) { } {{- end}}{{/* if .Features.ScanValueForEnum */}} {{end}} -` diff --git a/generator/golang/templates/file.go b/generator/golang/templates/file.go deleted file mode 100644 index b3e8a174..00000000 --- a/generator/golang/templates/file.go +++ /dev/null @@ -1,59 +0,0 @@ -// Copyright 2021 CloudWeGo Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package templates - -// File . -var File = `// Code generated by thriftgo ({{Version}}). DO NOT EDIT. -{{InsertionPoint "bof"}} - -package {{GetPackageName .AST}} - -import ( - {{InsertionPoint "imports"}} -) - -{{template "Constant" .}} - -{{- range .Enums}} -{{template "Enum" .}} -{{- end}} - -{{- range .Typedefs}} -{{template "Typedef" .}} -{{- end}} - -{{- range .Structs}} -{{template "StructLike" .}} -{{- end}} - -{{- range .Unions}} -{{template "StructLike" .}} -{{- end}} - -{{- range .Exceptions}} -{{template "StructLike" .}} -{{- end}} - -{{- range .Services}} -{{template "Service" .}} -{{template "Client" .}} -{{- end}} - -{{- range .Services}} -{{template "Processor" .}} -{{- end}} - -{{- InsertionPoint "eof"}} -` diff --git a/generator/golang/templates/file.go.tmpl b/generator/golang/templates/file.go.tmpl new file mode 100644 index 00000000..8861620c --- /dev/null +++ b/generator/golang/templates/file.go.tmpl @@ -0,0 +1,41 @@ +// Code generated by thriftgo ({{Version}}). DO NOT EDIT. +{{InsertionPoint "bof"}} + +package {{GetPackageName .AST}} + +import ( + {{InsertionPoint "imports"}} +) + +{{template "Constant" .}} + +{{- range .Enums}} +{{template "Enum" .}} +{{- end}} + +{{- range .Typedefs}} +{{template "Typedef" .}} +{{- end}} + +{{- range .Structs}} +{{template "StructLike" .}} +{{- end}} + +{{- range .Unions}} +{{template "StructLike" .}} +{{- end}} + +{{- range .Exceptions}} +{{template "StructLike" .}} +{{- end}} + +{{- range .Services}} +{{template "Service" .}} +{{template "Client" .}} +{{- end}} + +{{- range .Services}} +{{template "Processor" .}} +{{- end}} + +{{- InsertionPoint "eof"}} diff --git a/generator/golang/templates/imports.go b/generator/golang/templates/imports.go deleted file mode 100644 index edaba3f7..00000000 --- a/generator/golang/templates/imports.go +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright 2021 CloudWeGo Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package templates - -// Imports . -var Imports = ` -{{define "Imports"}} - {{- range $path, $alias := .}} - {{$alias }}"{{$path}}" - {{- end}} -{{end}} -` diff --git a/generator/golang/templates/imports.go.tmpl b/generator/golang/templates/imports.go.tmpl new file mode 100644 index 00000000..53a40b4a --- /dev/null +++ b/generator/golang/templates/imports.go.tmpl @@ -0,0 +1,5 @@ +{{define "Imports"}} + {{- range $path, $alias := .}} + {{$alias }}"{{$path}}" + {{- end}} +{{end}} diff --git a/generator/golang/templates/init.go b/generator/golang/templates/init.go index 5cb07fdd..3445bcfe 100644 --- a/generator/golang/templates/init.go +++ b/generator/golang/templates/init.go @@ -14,7 +14,11 @@ package templates -import "github.com/cloudwego/thriftgo/generator/golang/templates/slim" +import ( + _ "embed" + + "github.com/cloudwego/thriftgo/generator/golang/templates/slim" +) // Alternative returns all alternative templates. func Alternative() map[string][]string { @@ -23,38 +27,51 @@ func Alternative() map[string][]string { } } +// templates. +var ( + //go:embed file.go.tmpl + File string + + //go:embed client.go.tmpl + Client string + + //go:embed constant.go.tmpl + Constant string + + //go:embed deep_equal.go.tmpl + DeepEqual string + + //go:embed enum.go.tmpl + Enum string + + //go:embed imports.go.tmpl + Imports string + + //go:embed processor.go.tmpl + Processor string + + //go:embed service.go.tmpl + Service string + + //go:embed struct.go.tmpl + Struct string + + //go:embed typedef.go.tmpl + Typedef string +) + // Templates returns all templates defined in this package. func Templates() []string { return []string{ - File, Imports, Constant, Enum, Typedef, - StructLike, - StructLikeDefault, - StructLikeRead, - StructLikeReadField, - StructLikeWrite, - StructLikeWriteField, - FieldGetOrSet, - FieldIsSet, - FieldRead, - FieldReadStructLike, - FieldReadBaseType, - FieldReadContainer, - FieldReadMap, - FieldReadSet, - FieldReadList, - FieldWrite, - FieldWriteStructLike, - FieldWriteBaseType, - FieldWriteContainer, - FieldWriteMap, - FieldWriteSet, - FieldWriteList, - StructLikeDeepEqual, - StructLikeDeepEqualField, - FieldDeepEqual, - FieldDeepEqualBase, - FieldDeepEqualContainer, - FieldDeepEqualStructLike, - FunctionSignature, Service, Client, Processor, + Client, + Constant, + DeepEqual, + Enum, + File, + Imports, + Processor, + Service, + Struct, + Typedef, } } diff --git a/generator/golang/templates/init_test.go b/generator/golang/templates/init_test.go index 279c4281..5f0aa5cb 100644 --- a/generator/golang/templates/init_test.go +++ b/generator/golang/templates/init_test.go @@ -15,7 +15,6 @@ package templates_test import ( - "fmt" "log" "testing" "text/template" @@ -25,24 +24,10 @@ import ( "github.com/cloudwego/thriftgo/generator/golang/templates" ) -// forceSingleDefinition panics if the given template contains multiple definition. -// It is used to keep maintainability of templates. -// The result is the name defined in the given template. -func forceSingleDefinition(pkg, text string, funcMap template.FuncMap) string { - name := "force-single-definition" +func validate(pkg, text string, funcMap template.FuncMap) { + name := "template-validation" tpl := template.New(name).Funcs(funcMap) - tpl = template.Must(tpl.Parse(text)) - if tpls := tpl.Templates(); len(tpls) != 2 { - err := fmt.Errorf( - "templates must have only one definition: pkg[%q] `\n----%s----\n`", - pkg, text) - panic(err) - } else { - if tpls[0].Name() == name { - return tpls[1].Name() - } - return tpls[0].Name() - } + template.Must(tpl.Parse(text)) } var logFunc backend.LogFunc @@ -59,18 +44,18 @@ func init() { } } -func TestDefinitionNumber(t *testing.T) { +func TestTemplateValidation(t *testing.T) { utils := golang.NewCodeUtils(logFunc) funcs := utils.BuildFuncMap() for _, tpl := range templates.Templates() { if tpl != templates.File { - forceSingleDefinition("", tpl, funcs) + validate("", tpl, funcs) } } for pkg, tpls := range templates.Alternative() { for _, tpl := range tpls { if tpl != templates.File { - forceSingleDefinition(pkg, tpl, funcs) + validate(pkg, tpl, funcs) } } } diff --git a/generator/golang/templates/processor.go b/generator/golang/templates/processor.go.tmpl similarity index 88% rename from generator/golang/templates/processor.go rename to generator/golang/templates/processor.go.tmpl index f4d5c4d1..88d11f8e 100644 --- a/generator/golang/templates/processor.go +++ b/generator/golang/templates/processor.go.tmpl @@ -1,21 +1,3 @@ -// Copyright 2021 CloudWeGo Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package templates - -// Processor . -var Processor = ` {{define "Processor"}} {{- UseStdLibrary "thrift" "context"}} {{- $BasePrefix := ServicePrefix .Base}} @@ -175,4 +157,3 @@ func (p *{{$ProcessName}}) Process(ctx context.Context, seqId int32, iprot, opro {{- end}} {{- end}}{{/* range .Functions */}} {{- end}}{{/* define "Processor" */}} -` diff --git a/generator/golang/templates/service.go b/generator/golang/templates/service.go.tmpl similarity index 56% rename from generator/golang/templates/service.go rename to generator/golang/templates/service.go.tmpl index 7d212c25..2c70b993 100644 --- a/generator/golang/templates/service.go +++ b/generator/golang/templates/service.go.tmpl @@ -1,21 +1,3 @@ -// Copyright 2021 CloudWeGo Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package templates - -// FunctionSignature . -var FunctionSignature = ` {{define "FunctionSignature"}} {{- UseStdLibrary "context"}} {{- $Function := .}} @@ -27,10 +9,7 @@ var FunctionSignature = ` {{- if not .Void}}r {{.ResponseGoTypeName}}, {{- end -}} err error) {{- end}}{{/* define "FunctionSignature" */}} -` -// Service . -var Service = ` {{define "Service"}} {{- $BasePrefix := ServicePrefix .Base}} {{- $BaseService := ServiceName .Base}} @@ -48,4 +27,3 @@ type {{$ServiceName}} interface { {{- end}} } {{- end}}{{/* define "Service" */}} -` diff --git a/generator/golang/templates/slim/slim.go b/generator/golang/templates/slim/slim.go index 2cb8de2e..2b1ce95d 100644 --- a/generator/golang/templates/slim/slim.go +++ b/generator/golang/templates/slim/slim.go @@ -14,6 +14,8 @@ package slim +import _ "embed" + // Extension . func Extension() []string { return []string{ @@ -27,78 +29,8 @@ func Extension() []string { // Because text.Templates will not substitute an existing template with an empty one, // we use insertion points to walk around this problem and achieve deleting templates. var ( - StructLike = ` -{{define "StructLike"}} -{{- $TypeName := .GoName}} -{{InsertionPoint .Category .Name}} -{{- if and Features.ReserveComments .ReservedComments}}{{.ReservedComments}}{{end}} -type {{$TypeName}} struct { -{{- range .Fields}} - {{- InsertionPoint $.Category $.Name .Name}} - {{- if and Features.ReserveComments .ReservedComments}} - {{.ReservedComments}} - {{- end}} - {{(.GoName)}} {{.GoTypeName}} {{GenFieldTags . (InsertionPoint $.Category $.Name .Name "tag")}} -{{- end}} - {{- if Features.KeepUnknownFields}} - {{- UseStdLibrary "unknown"}} - _unknownFields unknown.Fields - {{- end}} -} - -{{- if Features.GenerateTypeMeta }} -{{- UseStdLibrary "meta"}} -func init() { - meta.RegisterStruct(New{{$TypeName}}, {{Marshal .}}) -} -{{- end}}{{/* if Features.GenerateTypeMeta */}} - -func New{{$TypeName}}() *{{$TypeName}} { - return &{{$TypeName}}{ - {{template "StructLikeDefault" .}} - } -} - -{{template "FieldGetOrSet" .}} - -{{if eq .Category "union"}} -func (p *{{$TypeName}}) CountSetFields{{$TypeName}}() int { - count := 0 - {{- range .Fields}} - {{- if SupportIsSet .Field}} - if p.{{.IsSetter}}() { - count++ - } - {{- end}} - {{- end}} - return count -} -{{- end}} - -{{if Features.KeepUnknownFields}} -func (p *{{$TypeName}}) CarryingUnknownFields() bool { - return len(p._unknownFields) > 0 -} -{{end}}{{/* if Features.KeepUnknownFields */}} - -{{template "FieldIsSet" .}} - -func (p *{{$TypeName}}) String() string { - if p == nil { - return "" - } - {{- UseStdLibrary "fmt"}} - return fmt.Sprintf("{{$TypeName}}(%+v)", *p) -} - -{{- if eq .Category "exception"}} -func (p *{{$TypeName}}) Error() string { - return p.String() -} -{{- end}} - -{{- end}}{{/* define "StructLike" */}} - ` + //go:embed struct.go.tmpl + StructLike string Client = ` {{define "Client"}} diff --git a/generator/golang/templates/slim/struct.go.tmpl b/generator/golang/templates/slim/struct.go.tmpl new file mode 100644 index 00000000..5c9abfe2 --- /dev/null +++ b/generator/golang/templates/slim/struct.go.tmpl @@ -0,0 +1,70 @@ +{{define "StructLike"}} +{{- $TypeName := .GoName}} +{{InsertionPoint .Category .Name}} +{{- if and Features.ReserveComments .ReservedComments}}{{.ReservedComments}}{{end}} +type {{$TypeName}} struct { +{{- range .Fields}} + {{- InsertionPoint $.Category $.Name .Name}} + {{- if and Features.ReserveComments .ReservedComments}} + {{.ReservedComments}} + {{- end}} + {{(.GoName)}} {{.GoTypeName}} {{GenFieldTags . (InsertionPoint $.Category $.Name .Name "tag")}} +{{- end}} + {{- if Features.KeepUnknownFields}} + {{- UseStdLibrary "unknown"}} + _unknownFields unknown.Fields + {{- end}} +} + +{{- if Features.GenerateTypeMeta }} +{{- UseStdLibrary "meta"}} +func init() { + meta.RegisterStruct(New{{$TypeName}}, {{Marshal .}}) +} +{{- end}}{{/* if Features.GenerateTypeMeta */}} + +func New{{$TypeName}}() *{{$TypeName}} { + return &{{$TypeName}}{ + {{template "StructLikeDefault" .}} + } +} + +{{template "FieldGetOrSet" .}} + +{{if eq .Category "union"}} +func (p *{{$TypeName}}) CountSetFields{{$TypeName}}() int { + count := 0 + {{- range .Fields}} + {{- if SupportIsSet .Field}} + if p.{{.IsSetter}}() { + count++ + } + {{- end}} + {{- end}} + return count +} +{{- end}} + +{{if Features.KeepUnknownFields}} +func (p *{{$TypeName}}) CarryingUnknownFields() bool { + return len(p._unknownFields) > 0 +} +{{end}}{{/* if Features.KeepUnknownFields */}} + +{{template "FieldIsSet" .}} + +func (p *{{$TypeName}}) String() string { + if p == nil { + return "" + } + {{- UseStdLibrary "fmt"}} + return fmt.Sprintf("{{$TypeName}}(%+v)", *p) +} + +{{- if eq .Category "exception"}} +func (p *{{$TypeName}}) Error() string { + return p.String() +} +{{- end}} + +{{- end}}{{/* define "StructLike" */}} diff --git a/generator/golang/templates/struct.go b/generator/golang/templates/struct.go.tmpl similarity index 90% rename from generator/golang/templates/struct.go rename to generator/golang/templates/struct.go.tmpl index 109206da..143db3d8 100644 --- a/generator/golang/templates/struct.go +++ b/generator/golang/templates/struct.go.tmpl @@ -1,21 +1,3 @@ -// Copyright 2021 CloudWeGo Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package templates - -// StructLike is the code template for struct, union, and exception. -var StructLike = ` {{define "StructLike"}} {{- $TypeName := .GoName}} {{InsertionPoint .Category .Name}} @@ -106,20 +88,15 @@ func (p *{{$TypeName}}) Error() string { {{- end}} {{- end}}{{/* define "StructLike" */}} -` -// StructLikeDefault is the code template for structure initialization. -var StructLikeDefault = ` {{- define "StructLikeDefault"}} {{- range .Fields}} {{- if .IsSetDefault}} {{.GoName}}: {{.DefaultValue}}, {{- end}} {{- end}} -{{- end -}}` +{{- end -}} -// StructLikeRead . -var StructLikeRead = ` {{define "StructLikeRead"}} {{- UseStdLibrary "thrift" "fmt"}} {{- $TypeName := .GoName}} @@ -229,10 +206,7 @@ RequiredFieldNotSetError: {{- end}}{{/* if $RequiredFieldNotSetError */}} } {{- end}}{{/* define "StructLikeRead" */}} -` -// StructLikeReadField . -var StructLikeReadField = ` {{define "StructLikeReadField"}} {{- UseStdLibrary "thrift"}} {{- $TypeName := .GoName}} @@ -245,10 +219,7 @@ func (p *{{$TypeName}}) {{.Reader}}(iprot thrift.TProtocol) error { } {{- end}}{{/* range .Fields */}} {{- end}}{{/* define "StructLikeReadField" */}} -` -// StructLikeWrite . -var StructLikeWrite = ` {{define "StructLikeWrite"}} {{- UseStdLibrary "thrift" "fmt"}} {{- $TypeName := .GoName}} @@ -305,10 +276,7 @@ UnknownFieldsWriteError: {{- end}}{{/* if Features.KeepUnknownFields */}} } {{- end}}{{/* define "StructLikeWrite" */}} -` -// StructLikeWriteField . -var StructLikeWriteField = ` {{define "StructLikeWriteField"}} {{- UseStdLibrary "thrift" "fmt"}} {{- $TypeName := .GoName}} @@ -339,10 +307,7 @@ WriteFieldEndError: } {{end}}{{/* range .Fields */}} {{- end}}{{/* define "StructLikeWriteField" */}} -` -// FieldGetOrSet . -var FieldGetOrSet = ` {{define "FieldGetOrSet"}} {{- $TypeName := .GoName}} {{- range .Fields}} @@ -408,10 +373,7 @@ func (p *{{$TypeName}}) {{$SetterName}}(val {{$FieldTypeName}}) { {{- end}}{{/* if Features.GenerateSetter */}} {{- end}}{{/* define "FieldGetOrSet" */}} -` -// FieldIsSet . -var FieldIsSet = ` {{define "FieldIsSet"}} {{- $TypeName := .GoName}} {{- range .Fields}} @@ -438,10 +400,7 @@ func (p *{{$TypeName}}) {{$IsSetName}}() bool { {{end}} {{- end}}{{/* range .Fields */}} {{- end}}{{/* define "FieldIsSet" */}} -` -// FieldRead . -var FieldRead = ` {{define "FieldRead"}} {{- if .Type.Category.IsStructLike}} {{- template "FieldReadStructLike" .}} @@ -451,20 +410,14 @@ var FieldRead = ` {{- template "FieldReadBaseType" .}} {{- end}} {{- end}}{{/* define "FieldRead" */}} -` -// FieldReadStructLike . -var FieldReadStructLike = ` {{define "FieldReadStructLike"}} {{- .Target}} {{if .NeedDecl}}:{{end}}= {{.TypeName.Deref.NewFunc}}() if err := {{.Target}}.Read(iprot); err != nil { return err } {{- end}}{{/* define "FieldReadStructLike" */}} -` -// FieldReadBaseType . -var FieldReadBaseType = ` {{define "FieldReadBaseType"}} {{- $DiffType := or .Type.Category.IsEnum .Type.Category.IsBinary}} {{- if .NeedDecl}} @@ -489,10 +442,7 @@ var FieldReadBaseType = ` {{- end}} } {{- end}}{{/* define "FieldReadBaseType" */}} -` -// FieldReadContainer . -var FieldReadContainer = ` {{define "FieldReadContainer"}} {{- if eq "Map" .TypeID}} {{- template "FieldReadMap" .}} @@ -502,10 +452,7 @@ var FieldReadContainer = ` {{- template "FieldReadSet" .}} {{- end}} {{- end}}{{/* define "FieldReadContainer" */}} -` -// FieldReadMap . -var FieldReadMap = ` {{define "FieldReadMap"}} _, _, size, err := iprot.ReadMapBegin() if err != nil { @@ -531,10 +478,7 @@ var FieldReadMap = ` return err } {{- end}}{{/* define "FieldReadMap" */}} -` -// FieldReadSet . -var FieldReadSet = ` {{define "FieldReadSet"}} _, size, err := iprot.ReadSetBegin() if err != nil { @@ -556,10 +500,7 @@ var FieldReadSet = ` return err } {{- end}}{{/* define "FieldReadSet" */}} -` -// FieldReadList . -var FieldReadList = ` {{define "FieldReadList"}} _, size, err := iprot.ReadListBegin() if err != nil { @@ -581,10 +522,7 @@ var FieldReadList = ` return err } {{- end}}{{/* define "FieldReadList" */}} -` -// FieldWrite . -var FieldWrite = ` {{define "FieldWrite"}} {{- if .Type.Category.IsStructLike}} {{- template "FieldWriteStructLike" .}} @@ -594,19 +532,13 @@ var FieldWrite = ` {{- template "FieldWriteBaseType" .}} {{- end}} {{- end}}{{/* define "FieldWrite" */}} -` -// FieldWriteStructLike . -var FieldWriteStructLike = ` {{define "FieldWriteStructLike"}} if err := {{.Target}}.Write(oprot); err != nil { return err } {{- end}}{{/* define "FieldWriteStructLike" */}} -` -// FieldWriteBaseType . -var FieldWriteBaseType = ` {{define "FieldWriteBaseType"}} {{- $Value := .Target}} {{- if .IsPointer}}{{$Value = printf "*%s" $Value}}{{end}} @@ -616,10 +548,7 @@ var FieldWriteBaseType = ` return err } {{- end}}{{/* define "FieldWriteBaseType" */}} -` -// FieldWriteContainer . -var FieldWriteContainer = ` {{define "FieldWriteContainer"}} {{- if eq "Map" .TypeID}} {{- template "FieldWriteMap" .}} @@ -629,10 +558,7 @@ var FieldWriteContainer = ` {{- template "FieldWriteSet" .}} {{- end}} {{- end}}{{/* define "FieldWriteContainer" */}} -` -// FieldWriteMap . -var FieldWriteMap = ` {{define "FieldWriteMap"}} if err := oprot.WriteMapBegin(thrift. {{- .KeyCtx.Type | GetTypeIDConstant -}} @@ -650,10 +576,7 @@ var FieldWriteMap = ` return err } {{- end}}{{/* define "FieldWriteMap" */}} -` -// FieldWriteSet . -var FieldWriteSet = ` {{define "FieldWriteSet"}} if err := oprot.WriteSetBegin(thrift. {{- .ValCtx.Type | GetTypeIDConstant -}} @@ -687,10 +610,7 @@ var FieldWriteSet = ` return err } {{- end}}{{/* define "FieldWriteSet" */}} -` -// FieldWriteList . -var FieldWriteList = ` {{define "FieldWriteList"}} if err := oprot.WriteListBegin(thrift. {{- .ValCtx.Type | GetTypeIDConstant -}} @@ -705,4 +625,3 @@ var FieldWriteList = ` return err } {{- end}}{{/* define "FieldWriteList" */}} -` diff --git a/generator/golang/templates/typedef.go b/generator/golang/templates/typedef.go deleted file mode 100644 index 6e58fcd2..00000000 --- a/generator/golang/templates/typedef.go +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright 2021 CloudWeGo Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package templates - -// Typedef . -var Typedef = ` -{{define "Typedef"}} -{{- $NewTypeName := .GoName}} -{{- $OldTypeName := .GoTypeName}} -{{- if and Features.ReserveComments .ReservedComments}}{{.ReservedComments}}{{end}} -{{- if Features.TypedefAsTypeAlias }} -type {{$NewTypeName}} = {{$OldTypeName}} -{{- else}} -type {{$NewTypeName}} {{$OldTypeName}} -{{- end}} - -{{if .Type.Category.IsStructLike}} -func New{{$NewTypeName}}() *{{$NewTypeName}} { - return {{$OldTypeName.NewFunc}}() -} -{{- end}}{{/* if .Type.Category.IsStructLike */}} -{{- end}}{{/* define "Typedef" */}} -` diff --git a/generator/golang/templates/typedef.go.tmpl b/generator/golang/templates/typedef.go.tmpl new file mode 100644 index 00000000..0346135c --- /dev/null +++ b/generator/golang/templates/typedef.go.tmpl @@ -0,0 +1,16 @@ +{{define "Typedef"}} +{{- $NewTypeName := .GoName}} +{{- $OldTypeName := .GoTypeName}} +{{- if and Features.ReserveComments .ReservedComments}}{{.ReservedComments}}{{end}} +{{- if Features.TypedefAsTypeAlias }} +type {{$NewTypeName}} = {{$OldTypeName}} +{{- else}} +type {{$NewTypeName}} {{$OldTypeName}} +{{- end}} + +{{if .Type.Category.IsStructLike}} +func New{{$NewTypeName}}() *{{$NewTypeName}} { + return {{$OldTypeName.NewFunc}}() +} +{{- end}}{{/* if .Type.Category.IsStructLike */}} +{{- end}}{{/* define "Typedef" */}}