-
Notifications
You must be signed in to change notification settings - Fork 4
/
address.go
128 lines (115 loc) · 2.63 KB
/
address.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
package ast
import (
"strings"
"github.com/dipdup-net/go-lib/tools/base"
"github.com/dipdup-net/go-lib/tools/consts"
"github.com/dipdup-net/go-lib/tools/forge"
)
//
// Address
//
// Address -
type Address struct {
Default
}
// NewAddress -
func NewAddress(depth int) *Address {
return &Address{
Default: NewDefault(consts.ADDRESS, 0, depth),
}
}
// ToBaseNode -
func (a *Address) ToBaseNode(optimized bool) (*base.Node, error) {
val := a.Value.(string)
if a.ValueKind == valueKindBytes {
return toBaseNodeBytes(val), nil
}
if optimized {
value, err := forge.Contract(val)
if err != nil {
return nil, err
}
return toBaseNodeBytes(value), nil
}
return toBaseNodeString(val), nil
}
// ToMiguel -
func (a *Address) ToMiguel() (*MiguelNode, error) {
name := a.GetTypeName()
var value string
if a.Value != nil {
value = a.Value.(string)
if a.ValueKind == valueKindBytes {
v, err := forge.UnforgeContract(value)
if err != nil {
return nil, err
}
value = v
}
}
return &MiguelNode{
Prim: a.Prim,
Type: strings.ToLower(a.Prim),
Value: value,
Name: &name,
}, nil
}
// GetJSONModel -
func (a *Address) GetJSONModel(model JSONModel) {
if a.Value != nil {
value := a.Value.(string)
if a.ValueKind == valueKindBytes {
v, err := forge.UnforgeContract(value)
if err == nil {
value = v
}
}
model[a.GetName()] = value
} else {
model[a.GetName()] = ""
}
}
// ToJSONSchema -
func (a *Address) ToJSONSchema() (*JSONSchema, error) {
return getAddressJSONSchema(a.Default), nil
}
// Compare -
func (a *Address) Compare(second Comparable) (int, error) {
secondAddress, ok := second.(*Address)
if !ok {
return 0, consts.ErrTypeIsNotComparable
}
if a.Value == secondAddress.Value {
return 0, nil
}
if a.ValueKind == secondAddress.ValueKind {
return strings.Compare(a.Value.(string), secondAddress.Value.(string)), nil
}
return compareNotOptimizedTypes(a.Default, secondAddress.Default, forge.Contract)
}
// Distinguish -
func (a *Address) Distinguish(x Distinguishable) (*MiguelNode, error) {
second, ok := x.(*Address)
if !ok {
return nil, nil
}
if err := a.optimizeStringValue(forge.UnforgeContract); err != nil {
return nil, err
}
if err := second.optimizeStringValue(forge.UnforgeContract); err != nil {
return nil, err
}
return a.Default.Distinguish(&second.Default)
}
// FromJSONSchema -
func (a *Address) FromJSONSchema(data map[string]interface{}) error {
setOptimizedJSONSchema(&a.Default, data, forge.UnforgeContract)
return nil
}
// FindByName -
func (a *Address) FindByName(name string, isEntrypoint bool) Node {
if a.GetName() == name {
return a
}
return nil
}