-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstructure.go
58 lines (46 loc) · 1.58 KB
/
structure.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
package capsule
import (
"github.com/michaelquigley/cf"
"github.com/pkg/errors"
)
type StructureDef struct {
Models []*ModelDef
}
type ModelDef struct {
Id string
Builder interface{}
}
func LoadStructureDef(path string) (*StructureDef, error) {
def := &StructureDef{}
options := cf.DefaultOptions()
for k, v := range structureBuilderRegistry {
options.AddFlexibleSetter(k, v)
}
if err := cf.BindYaml(def, path, options); err != nil {
return nil, errors.Wrapf(err, "error loading structure def from '%v' (%v)", path, err)
}
return def, nil
}
// Structure defines the interface that must be implemented by all structure models.
//
type Structure interface {
// Dump emits a string representation of the structure model for use in dump debugging output.
//
Dump() string
}
// StructureBuilder is used to create structures suited for capturing additional details about a capsule structure.
// When a structure definition is encountered in the tree, the models are built using the Build method below. Build
// takes a previous version of the identified model, allows additional transformation to be done to it, and emits an
// altered version to insert back into the model.
//
type StructureBuilder interface {
Build(rootPath string, node *Node, prev Structure) (Structure, error)
}
func RegisterStructureBuilder(id string, fs cf.FlexibleSetter) {
if structureBuilderRegistry == nil {
structureBuilderRegistry = make(map[string]cf.FlexibleSetter)
}
structureBuilderRegistry[id] = fs
}
var structureBuilderRegistry map[string]cf.FlexibleSetter
const StructureFeature = "structure.yaml"