-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgost.go
49 lines (41 loc) · 823 Bytes
/
gost.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
package gost
import (
"encoding/json"
"fmt"
)
type Gost struct {
values map[string][]byte
}
type GostKind string
const (
DefaultKind GostKind = "DEFAULT"
)
func GetGost(kind ...GostKind) (*Gost, error) {
if len(kind) == 0 {
kind = []GostKind{DefaultKind}
}
if len(kind) > 1 {
return nil, fmt.Errorf("too many kinds")
}
switch kind[0] {
case DefaultKind:
return &Gost{values: map[string][]byte{}}, nil
default:
return nil, fmt.Errorf("not a GostKind")
}
}
func (g *Gost) Put(value interface{}, key string) error {
val, err := json.Marshal(value)
if err != nil {
return err
}
g.values[key] = val
return nil
}
func (g *Gost) Get(resp interface{}, key string) error {
val, ok := g.values[key]
if !ok {
return fmt.Errorf("no value for key %q", key)
}
return json.Unmarshal(val, &resp)
}