forked from gagliardetto/anchor-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
169 lines (148 loc) · 3.24 KB
/
config.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
package main
import (
"errors"
"fmt"
. "github.com/gagliardetto/utilz"
)
var conf = &Config{}
type Config struct {
Encoding EncoderName
TypeID TypeIDName
Debug bool
DstDir string
ModPath string
RemoveAccountSuffix bool
}
func GetConfig() *Config {
return conf
}
// Validate validates
func (cfg *Config) Validate() error {
if cfg == nil {
return errors.New("cfg is nil")
}
if !isValidEncoder(cfg.Encoding) {
return fmt.Errorf("Encoder kind is not valid: %q", cfg.Encoding)
}
if !isValidTypeIDName(cfg.TypeID) {
return fmt.Errorf("TypeID kind is not valid: %q", cfg.TypeID)
}
return nil
}
func isValidEncoder(enc EncoderName) bool {
return SliceContains(
[]string{
string(EncodingBorsh),
string(EncodingBin),
string(EncodingCompactU16),
},
string(enc),
)
}
type TypeIDName string
const (
TypeIDUvarint32 TypeIDName = "uvarint32"
TypeIDUint32 TypeIDName = "uint32"
TypeIDUint8 TypeIDName = "uint8"
TypeIDAnchor TypeIDName = "anchor"
TypeIDNoType TypeIDName = "notype"
)
func isValidTypeIDName(typeID TypeIDName) bool {
return SliceContains(
[]string{
string(TypeIDUvarint32),
string(TypeIDUint32),
string(TypeIDUint8),
string(TypeIDAnchor),
string(TypeIDNoType),
},
string(typeID),
)
}
type TypeIDNameSlice []TypeIDName
func (slice TypeIDNameSlice) Has(v TypeIDName) bool {
for _, enc := range slice {
if v == enc {
return true
}
}
return false
}
func (name TypeIDName) On(
candidates TypeIDNameSlice,
fn func(),
) TypeIDName {
if candidates.Has(GetConfig().TypeID) {
fn()
}
return name
}
type EncoderName string
const (
// github.com/gagliardetto/binary: NewBinEncoder, NewBinDecoder
EncodingBin EncoderName = "bin"
// github.com/gagliardetto/binary: NewBorshEncoder, NewBorshDecoder
EncodingBorsh EncoderName = "borsh"
// https://docs.solana.com/developing/programming-model/transactions#compact-array-format
EncodingCompactU16 EncoderName = "compact-u16"
)
func (name EncoderName) _NewEncoder() string {
switch enc := GetConfig().Encoding; enc {
case EncodingBin:
return "NewBinEncoder"
case EncodingBorsh:
return "NewBorshEncoder"
case EncodingCompactU16:
return "NewCompact16Encoder"
default:
panic(enc)
}
}
func (name EncoderName) _NewDecoder() string {
switch enc := GetConfig().Encoding; enc {
case EncodingBin:
return "NewBinDecoder"
case EncodingBorsh:
return "NewBorshDecoder"
case EncodingCompactU16:
return "NewCompact16Decoder"
default:
panic(enc)
}
}
type EncoderNameSlice []EncoderName
func (slice EncoderNameSlice) Has(v EncoderName) bool {
for _, enc := range slice {
if v == enc {
return true
}
}
return false
}
func (name EncoderName) On(
anyEncoding EncoderNameSlice,
fn func(),
) EncoderName {
if anyEncoding.Has(GetConfig().Encoding) {
fn()
}
return name
}
func (name EncoderName) OnEncodingBin(fn func()) EncoderName {
if GetConfig().Encoding == EncodingBin {
fn()
}
return name
}
func (name EncoderName) OnEncodingBorsh(fn func()) EncoderName {
if GetConfig().Encoding == EncodingBorsh {
fn()
}
return name
}
func (name EncoderName) OnEncodingCompactU16(fn func()) EncoderName {
if GetConfig().Encoding == EncodingCompactU16 {
fn()
}
return name
}