-
Notifications
You must be signed in to change notification settings - Fork 536
/
field.go
175 lines (155 loc) · 4.9 KB
/
field.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Package field provides methods to parse a field provided in a command with the format name:type
package field
import (
"fmt"
"github.com/emicklei/proto"
"github.com/ignite/cli/v29/ignite/pkg/multiformatname"
"github.com/ignite/cli/v29/ignite/templates/field/datatype"
)
// Field represents a field inside a structure for a component
// it can be a field contained in a type or inside the response of a query, etc...
type Field struct {
Name multiformatname.Name
DatatypeName datatype.Name
Datatype string
}
// DataType returns the field Datatype.
func (f Field) DataType() string {
dt, ok := datatype.IsSupportedType(f.DatatypeName)
if !ok {
panic(fmt.Sprintf("unknown type %s", f.DatatypeName))
}
return dt.DataType(f.Datatype)
}
// ProtoFieldName returns the field name used in proto.
func (f Field) ProtoFieldName() string {
return f.Name.LowerCamel
}
// ProtoType returns the field proto Datatype.
func (f Field) ProtoType(index int) string {
dt, ok := datatype.IsSupportedType(f.DatatypeName)
if !ok {
panic(fmt.Sprintf("unknown type %s", f.DatatypeName))
}
return dt.ProtoType(f.Datatype, f.ProtoFieldName(), index)
}
// CollectionsKeyValueType returns the field collections key value type.
func (f Field) CollectionsKeyValueType() string {
dt, ok := datatype.IsSupportedType(f.DatatypeName)
if !ok {
panic(fmt.Sprintf("unknown type %s", f.DatatypeName))
}
return dt.CollectionsKeyValueName(f.Datatype)
}
// DefaultTestValue returns the Datatype value default.
func (f Field) DefaultTestValue() string {
dt, ok := datatype.IsSupportedType(f.DatatypeName)
if !ok {
panic(fmt.Sprintf("unknown type %s", f.DatatypeName))
}
return dt.DefaultTestValue
}
// ValueLoop returns the Datatype value for loop iteration.
func (f Field) ValueLoop() string {
dt, ok := datatype.IsSupportedType(f.DatatypeName)
if !ok {
panic(fmt.Sprintf("unknown type %s", f.DatatypeName))
}
if dt.NonIndex {
panic(fmt.Sprintf("non index type %s", f.DatatypeName))
}
return dt.ValueLoop
}
// ValueIndex returns the Datatype value for indexes.
func (f Field) ValueIndex() string {
dt, ok := datatype.IsSupportedType(f.DatatypeName)
if !ok {
panic(fmt.Sprintf("unknown type %s", f.DatatypeName))
}
if dt.NonIndex {
panic(fmt.Sprintf("non index type %s", f.DatatypeName))
}
return dt.ValueIndex
}
// ValueInvalidIndex returns the Datatype value for invalid indexes.
func (f Field) ValueInvalidIndex() string {
dt, ok := datatype.IsSupportedType(f.DatatypeName)
if !ok {
panic(fmt.Sprintf("unknown type %s", f.DatatypeName))
}
if dt.NonIndex {
panic(fmt.Sprintf("non index type %s", f.DatatypeName))
}
return dt.ValueInvalidIndex
}
// GenesisArgs returns the Datatype genesis args.
func (f Field) GenesisArgs(value int) string {
dt, ok := datatype.IsSupportedType(f.DatatypeName)
if !ok {
panic(fmt.Sprintf("unknown type %s", f.DatatypeName))
}
return dt.GenesisArgs(f.Name, value)
}
// CLIArgs returns the Datatype CLI args.
// TODO(@julienrbrt): Once unused and fully replaced by AutoCLI, remove CLIArgs from DataType.
func (f Field) CLIArgs(prefix string, argIndex int) string {
dt, ok := datatype.IsSupportedType(f.DatatypeName)
if !ok {
panic(fmt.Sprintf("unknown type %s", f.DatatypeName))
}
return dt.CLIArgs(f.Name, f.Datatype, prefix, argIndex)
}
// ToBytes returns the Datatype byte array cast.
func (f Field) ToBytes(name string) string {
dt, ok := datatype.IsSupportedType(f.DatatypeName)
if !ok {
panic(fmt.Sprintf("unknown type %s", f.DatatypeName))
}
if dt.NonIndex {
panic(fmt.Sprintf("non index type %s", f.DatatypeName))
}
return dt.ToBytes(name)
}
// ToString returns the Datatype byte array cast.
func (f Field) ToString(name string) string {
dt, ok := datatype.IsSupportedType(f.DatatypeName)
if !ok {
panic(fmt.Sprintf("unknown type %s", f.DatatypeName))
}
if dt.NonIndex {
panic(fmt.Sprintf("non index type %s", f.DatatypeName))
}
return dt.ToString(name)
}
// ToProtoField returns the Datatype as a *proto.Field node.
func (f Field) ToProtoField(index int) *proto.NormalField {
// TODO: Do we can if it's an index type?
dt, ok := datatype.IsSupportedType(f.DatatypeName)
if !ok {
panic(fmt.Sprintf("unknown type %s", f.DatatypeName))
}
return dt.ToProtoField(f.Datatype, f.Name.LowerCamel, index)
}
// GoCLIImports returns the Datatype imports for CLI package.
func (f Field) GoCLIImports() []datatype.GoImport {
dt, ok := datatype.IsSupportedType(f.DatatypeName)
if !ok {
panic(fmt.Sprintf("unknown type %s", f.DatatypeName))
}
return dt.GoCLIImports
}
// ProtoImports returns the Datatype imports for proto files.
func (f Field) ProtoImports() []string {
dt, ok := datatype.IsSupportedType(f.DatatypeName)
if !ok {
panic(fmt.Sprintf("unknown type %s", f.DatatypeName))
}
return dt.ProtoImports
}
// Value returns the field assign value.
func (f Field) Value() string {
if f.DataType() == "string" {
return fmt.Sprintf(`"%s"`, f.Name.Snake)
}
return f.ValueIndex()
}