forked from tliron/puccini
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype.go
74 lines (57 loc) · 1.54 KB
/
type.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
package normal
import (
"github.com/tliron/puccini/tosca"
)
//
// Type
//
type Type struct {
Name string `json:"-" yaml:"-"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
Metadata map[string]string `json:"metadata,omitempty" yaml:"metadata,omitempty"`
Parent string `json:"parent,omitempty" yaml:"parent,omitempty"`
}
func NewType(name string) *Type {
return &Type{
Name: name,
Metadata: make(map[string]string),
}
}
//
// Types
//
type Types map[string]*Type
func NewTypes(names ...string) Types {
types := make(Types)
for _, name := range names {
types[name] = NewType(name)
}
return types
}
func GetHierarchyTypes(hierarchy *tosca.Hierarchy) Types {
types := make(Types)
hierarchy.Range(func(entityPtr tosca.EntityPtr, parentEntityPtr tosca.EntityPtr) bool {
type_ := NewType(tosca.GetCanonicalName(entityPtr))
if parentEntityPtr != nil {
type_.Parent = tosca.GetCanonicalName(parentEntityPtr)
}
type_.Description, _ = tosca.GetDescription(entityPtr)
if metadata, ok := tosca.GetMetadata(entityPtr); ok {
for name, value := range metadata {
// No need to include "canonical_name" metadata
if name != "canonical_name" {
type_.Metadata[name] = value
}
}
}
types[type_.Name] = type_
return true
})
return types
}
func GetTypes(hierarchy *tosca.Hierarchy, entityPtr tosca.EntityPtr) (Types, bool) {
if childHierarchy, ok := hierarchy.Find(entityPtr); ok {
return GetHierarchyTypes(childHierarchy), true
}
return nil, false
}