-
Notifications
You must be signed in to change notification settings - Fork 2
/
template.go
130 lines (112 loc) · 2.8 KB
/
template.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package ent2ogen
import (
"embed"
"fmt"
"text/template"
"entgo.io/ent/entc/gen"
"github.com/go-faster/errors"
"github.com/ogen-go/ogen/gen/ir"
)
var (
//go:embed _templates
templateDir embed.FS
// funcMap contains extra template functions used by ent2ogen.
funcMap = template.FuncMap{
"sprintf": fmt.Sprintf,
"errorf": func(format string, args ...interface{}) (interface{}, error) {
return nil, fmt.Errorf(format, args...)
},
"assign": assign,
"unassign": unassign,
"rendertype": rendertype,
}
// templates holds all templates used by ent2ogen.
templates = gen.MustParse(gen.NewTemplate("ent2ogen").Funcs(funcMap).ParseFS(templateDir, "_templates/*.tmpl"))
)
func assign(dst *ir.Field, src *gen.Field) (string, error) {
var (
assignT = "t." + dst.Name
srcT = "e." + src.StructField()
)
if src.Nillable {
srcT = "*" + srcT
}
if dst.Type.IsGeneric() {
if dst.Type.GenericOf.IsPrimitive() {
return assignT + ".SetTo(" + srcT + ")", nil
}
gotyp := dst.Type.GenericOf.Go()
return fmt.Sprintf("%s.SetTo(openapi.%s(%s))", assignT, gotyp, srcT), nil
}
if dst.Type.IsEnum() {
gotyp := dst.Type.Go()
return fmt.Sprintf("%s = openapi.%s(%s)", assignT, gotyp, srcT), nil
}
return fmt.Sprintf("%s = %s", assignT, srcT), nil
}
func unassign(dst *ir.Field) (string, error) {
assignT := "t." + dst.Name
type assignStmt struct {
variable string
value string
}
var stmts []assignStmt
if dst.Type.IsGeneric() {
if dst.Type.GenericVariant.Optional {
// Reset ogen's generic 'Set' field.
//
// t.Foo.Set = false
stmts = append(stmts, assignStmt{
variable: fmt.Sprintf("%s.Set", assignT),
value: "false",
})
}
if dst.Type.GenericVariant.Nullable {
// Reset ogen's generic 'Null' field.
//
// t.Foo.Null = true
stmts = append(stmts, assignStmt{
variable: fmt.Sprintf("%s.Null", assignT),
value: "true",
})
}
}
// From github.com/ogen-go/ogen/gen/generics.go:boxType
if dst.Type.GenericOf.Kind == ir.KindArray || dst.Type.GenericOf.Primitive == ir.ByteSlice {
if dst.Type.GenericVariant.NullableOptional() {
return "", errors.New("unexpected nullable & optional ogen array type")
}
// t.Foo = nil
stmts = append(stmts, assignStmt{
variable: assignT,
value: "nil",
})
}
// Represent assignments in a shorter form:
// foo, bar = true, false
oneliner := ""
for i, stmt := range stmts {
oneliner += stmt.variable
if i+1 != len(stmts) {
oneliner += ", "
}
}
oneliner += " = "
for i, stmt := range stmts {
oneliner += stmt.value
if i+1 != len(stmts) {
oneliner += ", "
}
}
return oneliner, nil
}
func rendertype(t *ir.Type) string {
switch t.Kind {
case ir.KindPrimitive:
return t.Go()
case ir.KindArray:
return "[]" + rendertype(t.Item)
default:
return "openapi." + t.Go()
}
}