-
Notifications
You must be signed in to change notification settings - Fork 6
/
load.go
83 lines (79 loc) · 3.5 KB
/
load.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
package load
import "github.com/pcelvng/go-config/util/node"
type Loader interface {
// Load expects a struct pointer and will read in
// the config values into the underlying struct.
//
// It's possible and expected that the struct will already be populated with
// at least some values. Load should not clear out existing values
// if a replacing value does not exist.
//
// Load should support embedded structs and struct types.
//
// Load should ignore reading in values where the public member has a struct tag
// of this type of dash '-'. So, if the implementation were for loading flags
// and a 'flag' struct tag was provided as `flag:"-"` then no value should be read
// in to that struct member.
//
// Load should also ignore reading in values that have the 'config' struct tag
// with an 'ignore' value.
//
// Load is not responsible for validation other than correct formatting
// and type matching.
//
// Load should have specific support for reading in and parsing time.Time values
// and time.Duration. time.Time values should also support the 'time' tag value
// that specifies the time.Time expected format. The default format should be time.RFC3339.
// The 'time' tag should accept a raw format or a standard format available in the time
// standard library module. For example, instead of specifying "Mon, 02 Jan 2006 15:04:05 MST"
// as the time tag value, the user could specify "RFC1123". If the provided time value
// is incorrect, then Load should return an error indicating what the correct format is
// and that the time format is incorrect. time.Duration type values should be read in as time duration
// parsable strings.
//
// Load should be implemented with the fact in mind that other Loaders could
// be called before or after it. Therefore, Load does not return an error if a 'req'
// field value is not provided since that value could have already been provided or will
// be provided by a call to a different Loader.
Load([]byte, []*node.Nodes) error
}
type Unloader interface {
// Unload will express the underlying provided pointer struct as a series of
// pre-formatted bytes.
//
// If the underlying Unload implementation were for JSON then the returned bytes
// would be indented json.
//
// All, non-hidden values should be represented in the returned bytes even if no default
// value is specified. If a default value is specified then that value should be pre-populated
// in the returned bytes template.
//
// Unload should support the following struct tags:
// - config (config="ignore" ignore the field)
// - req (communicates if the field is required)
// - desc (field description; most likely expressed as a comment)
// - format specific tag (ie "env" for environment variables)
// - time (for time.Time field types)
//
// Additionally Unload will ignore the following field types:
// - functions
// - interfaces
// - complex64, complex128
// - arrays (slices are ok)
// - channels
// - private fields
//
// Generally speaking, Load and Unload should be idempotent. That is, the generated template
// from Unload should produce identical struct values when read back in by Load.
//
// time.Time field types should provide some type of hint to the user what the requested
// time format is. Most likely this will look like a comment with the specified time format.
//
// time.Duration type should be supported with default values time.Duration as string parsable
// values.
Unload([]*node.Nodes) ([]byte, error)
}
type LoadUnloader interface {
Loader
Unloader
}