-
Notifications
You must be signed in to change notification settings - Fork 20
/
entity-type.go
74 lines (57 loc) · 1.74 KB
/
entity-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/parsing"
)
//
// EntityType
//
type EntityType 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 NewEntityType(name string) *EntityType {
return &EntityType{
Name: name,
Metadata: make(map[string]string),
}
}
//
// EntityTypes
//
type EntityTypes map[string]*EntityType
func NewEntityTypes(names ...string) EntityTypes {
entityTypes := make(EntityTypes)
for _, name := range names {
entityTypes[name] = NewEntityType(name)
}
return entityTypes
}
func GetHierarchyEntityTypes(hierarchy *parsing.Hierarchy) EntityTypes {
entityTypes := make(EntityTypes)
hierarchy.Range(func(entityPtr parsing.EntityPtr, parentEntityPtr parsing.EntityPtr) bool {
entityType := NewEntityType(parsing.GetCanonicalName(entityPtr))
if parentEntityPtr != nil {
entityType.Parent = parsing.GetCanonicalName(parentEntityPtr)
}
entityType.Description, _ = parsing.GetDescription(entityPtr)
if metadata, ok := parsing.GetMetadata(entityPtr); ok {
for name, value := range metadata {
// No need to include "canonical_name" metadata
if name != "canonical_name" {
entityType.Metadata[name] = value
}
}
}
entityTypes[entityType.Name] = entityType
return true
})
return entityTypes
}
func GetEntityTypes(hierarchy *parsing.Hierarchy, entityPtr parsing.EntityPtr) (EntityTypes, bool) {
if childHierarchy, ok := hierarchy.Find(entityPtr); ok {
return GetHierarchyEntityTypes(childHierarchy), true
}
return nil, false
}