-
Notifications
You must be signed in to change notification settings - Fork 0
/
key_types.go
124 lines (95 loc) · 3.2 KB
/
key_types.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
// Copyright 2020 dfuse Platform Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package server
import (
"encoding/binary"
"encoding/hex"
"strconv"
zsw "github.com/zhongshuwen/zswchain-go"
)
type KeyConverter interface {
FromString(key string) (uint64, error)
ToString(key uint64) (string, error)
}
var keyTypeToKeyConverter = map[string]KeyConverter{
"uint64": &Uint64KeyConverter{},
"name": &NameKeyConverter{},
"symbol": &SymbolKeyConverter{},
"symbol_code": &SymbolCodeKeyConverter{},
"hex": &HexKeyConverter{},
"hex_be": &HexBEKeyConverter{},
}
type Uint64KeyConverter struct{}
func (c *Uint64KeyConverter) FromString(key string) (uint64, error) {
return strconv.ParseUint(key, 10, 64)
}
func (c *Uint64KeyConverter) ToString(key uint64) (string, error) {
return strconv.FormatUint(key, 10), nil
}
type NameKeyConverter struct{}
func (c *NameKeyConverter) FromString(key string) (uint64, error) {
return zsw.ExtendedStringToName(key)
}
func (c *NameKeyConverter) ToString(key uint64) (string, error) {
return zsw.NameToString(key), nil
}
type SymbolKeyConverter struct{}
func (c *SymbolKeyConverter) FromString(key string) (uint64, error) {
symbol, err := zsw.StringToSymbol(key)
if err != nil {
return 0, err
}
return symbol.ToUint64()
}
func (c *SymbolKeyConverter) ToString(key uint64) (string, error) {
return zsw.NewSymbolFromUint64(key).String(), nil
}
type SymbolCodeKeyConverter struct{}
func (c *SymbolCodeKeyConverter) FromString(key string) (uint64, error) {
symbolCode, err := zsw.StringToSymbolCode(key)
if err != nil {
return 0, err
}
return uint64(symbolCode), nil
}
func (c *SymbolCodeKeyConverter) ToString(key uint64) (string, error) {
return zsw.SymbolCode(key).String(), nil
}
type HexKeyConverter struct{}
func (c *HexKeyConverter) FromString(key string) (uint64, error) {
return strconv.ParseUint(key, 16, 64)
}
func (c *HexKeyConverter) ToString(key uint64) (string, error) {
keyBuffer := make([]byte, 8)
binary.LittleEndian.PutUint64(keyBuffer, key)
return hex.EncodeToString(keyBuffer), nil
}
type HexBEKeyConverter struct{}
func (c *HexBEKeyConverter) FromString(key string) (uint64, error) {
// FIXME: Provablty need invertiing the chars
return strconv.ParseUint(key, 16, 64)
}
func (c *HexBEKeyConverter) ToString(key uint64) (string, error) {
keyBuffer := make([]byte, 8)
binary.LittleEndian.PutUint64(keyBuffer, key)
return hex.EncodeToString(keyBuffer), nil
}
func getKeyConverterForType(keyType string) KeyConverter {
keyConverter, exists := keyTypeToKeyConverter[keyType]
if !exists {
// Name is always the default key converter whatever happen
keyConverter = keyTypeToKeyConverter["name"]
}
return keyConverter
}