-
Notifications
You must be signed in to change notification settings - Fork 46
/
gconf.go
79 lines (68 loc) · 2.13 KB
/
gconf.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
package gconf
import (
"encoding/json"
"fmt"
"time"
"github.com/iov-one/weave"
"github.com/iov-one/weave/x"
)
type Store interface {
Get([]byte) []byte
}
// Int returns an integer value stored under given name.
// This function panics if configuration cannot be acquired.
func Int(confStore Store, propName string) int {
var value int
loadInto(confStore, propName, &value)
return value
}
// Duration returns a duration value stored under given name.
// This function panics if configuration cannot be acquired.
func Duration(confStore Store, propName string) time.Duration {
var value time.Duration
loadInto(confStore, propName, &value)
return value
}
// String returns a string value stored under given name.
// This function panics if configuration cannot be acquired.
func String(confStore Store, propName string) string {
var value string
loadInto(confStore, propName, &value)
return value
}
// Strings returns an array of string value stored under given name.
// This function panics if configuration cannot be acquired.
func Strings(confStore Store, propName string) []string {
var value []string
loadInto(confStore, propName, &value)
return value
}
// Address returns an address value stored under given name.
// This function panics if configuration cannot be acquired.
func Address(confStore Store, propName string) weave.Address {
var value weave.Address
loadInto(confStore, propName, &value)
return value
}
// Bytes returns a bytes value stored under given name.
// This function panics if configuration cannot be acquired.
func Bytes(confStore Store, propName string) []byte {
value := make([]byte, 0, 128)
loadInto(confStore, propName, &value)
return value
}
func Coin(confStore Store, propName string) x.Coin {
var value x.Coin
loadInto(confStore, propName, &value)
return value
}
func loadInto(confStore Store, propName string, dest interface{}) {
key := []byte("gconf:" + propName)
raw := confStore.Get(key)
if raw == nil {
panic(fmt.Sprintf("cannot load %q configuration: not found", propName))
}
if err := json.Unmarshal(raw, dest); err != nil {
panic(fmt.Sprintf("cannot load %q configuration: %s", propName, err))
}
}