-
Notifications
You must be signed in to change notification settings - Fork 4
/
list.go
63 lines (50 loc) · 1.5 KB
/
list.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
package types
import (
"fmt"
"github.com/dave/jennifer/jen"
"github.com/dipdup-net/go-lib/tzkt/data"
)
// List -
type List struct{}
// AsField -
func (List) AsField(name, path string, schema data.JSONSchema, isRequired bool, result *ContractTypeResult) (jen.Code, error) {
tags := map[string]string{
"json": name,
}
if isRequired {
tags["validate"] = TagRequired
}
return jen.Id(fieldName("List", name)).Op("[]").Add(jen.Id(result.GetName("List", name))).Tag(tags), nil
}
// AsCode -
func (l List) AsCode(name, path string, schema data.JSONSchema, result *ContractTypeResult) (Code, error) {
code, err := l.createList(name, path, schema, result)
if err != nil {
return code, err
}
statement := jen.Comment(code.Name).Line().Type().Id(code.Name)
return Code{
Statement: statement.Add(code.Statement),
Name: code.Name,
}, nil
}
// AsType -
func (l List) AsType(name, path string, schema data.JSONSchema, result *ContractTypeResult) (Code, error) {
return l.createList(name, path, schema, result)
}
func (List) createList(name, path string, schema data.JSONSchema, result *ContractTypeResult) (Code, error) {
var code Code
code.Name = result.GetName("List", name)
if schema.Items != nil {
typ, err := selectType(*schema.Items)
if err != nil {
return code, err
}
itemType, err := typ.AsType(fmt.Sprintf("%s_item", name), getPath(path, name), *schema.Items, result)
if err != nil {
return code, err
}
code.Statement = jen.Op("[]").Add(itemType.Statement)
}
return code, nil
}