Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(golang): mv templates into files for embedding #47

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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}}
Expand Down Expand Up @@ -106,4 +88,3 @@ func (p *{{$ClientName}}) {{- template "FunctionSignature" . -}} {
}
{{- end}}{{/* range .Functions */}}
{{- end}}{{/* define "Cleint" */}}
`
Original file line number Diff line number Diff line change
@@ -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}}
Expand All @@ -41,4 +23,3 @@ var (
)
{{- end}}
{{end}}{{- /* define "Constant" */ -}}
`
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -32,10 +14,7 @@ func (p *{{$TypeName}}) DeepEqual(ano *{{$TypeName}}) bool {
return true
}
{{- end}}{{/* "StructLikeDeepEqual" */}}
`

// StructLikeDeepEqualField .
var StructLikeDeepEqualField = `
{{define "StructLikeDeepEqualField"}}
{{- $TypeName := .GoName}}
{{- range .Fields}}
Expand All @@ -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" .}}
Expand All @@ -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}} {
Expand Down Expand Up @@ -99,10 +69,7 @@ var FieldDeepEqualBase = `
}
{{- end}}{{/* if .Type.Category.IsString */}}
{{- end}}{{/* "FieldDeepEqualBase" */}}
`

// FieldDeepEqualContainer .
var FieldDeepEqualContainer = `
{{define "FieldDeepEqualContainer"}}
{{- if .IsPointer}}
if {{.Target}} == {{.Source}} {
Expand All @@ -123,4 +90,3 @@ var FieldDeepEqualContainer = `
{{- template "FieldDeepEqual" $ctx}}
}
{{- end}}{{/* "FieldDeepEqualContainer" */}}
`
Original file line number Diff line number Diff line change
@@ -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}}
Expand Down Expand Up @@ -86,4 +68,3 @@ func (p *{{$EnumType}}) Value() (driver.Value, error) {
}
{{- end}}{{/* if .Features.ScanValueForEnum */}}
{{end}}
`
59 changes: 0 additions & 59 deletions generator/golang/templates/file.go

This file was deleted.

41 changes: 41 additions & 0 deletions generator/golang/templates/file.go.tmpl
Original file line number Diff line number Diff line change
@@ -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"}}
24 changes: 0 additions & 24 deletions generator/golang/templates/imports.go

This file was deleted.

5 changes: 5 additions & 0 deletions generator/golang/templates/imports.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{{define "Imports"}}
{{- range $path, $alias := .}}
{{$alias }}"{{$path}}"
{{- end}}
{{end}}
79 changes: 48 additions & 31 deletions generator/golang/templates/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
}
}
Loading