-
Notifications
You must be signed in to change notification settings - Fork 3
/
ChronicleTemplate.go
130 lines (110 loc) · 3.6 KB
/
ChronicleTemplate.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"fmt"
"sort"
"strings"
)
// ChronicleTemplate represents a template configuration for chronicles. It contains
// information on what to put where.
type ChronicleTemplate struct {
id string
description string
inherit string
yFilename string // filename of the originating yaml file
content map[string]ContentEntry
presets map[string]ContentEntry
}
// NewChronicleTemplate converts a YamlFile into a ChronicleTemplate. It returns
// an error if the YamlFile cannot be converted to a ChronicleTemplate, e.g. because
// it is missing required entries.
func NewChronicleTemplate(yFilename string, yFile *YamlFile) (ct *ChronicleTemplate, err error) {
if !IsSet(yFilename) {
return nil, fmt.Errorf("No filename provided")
}
if yFile == nil {
return nil, fmt.Errorf("Provided YamlFile object is nil")
}
if !IsSet(yFile.ID) {
return nil, fmt.Errorf("Template file '%v' does not contain an ID", yFilename)
}
if !IsSet(yFile.Description) {
return nil, fmt.Errorf("Template file '%v' does not contain a description", yFilename)
}
ct = new(ChronicleTemplate)
ct.id = yFile.ID
ct.description = yFile.Description
ct.inherit = yFile.Inherit
ct.yFilename = yFilename
ct.content = make(map[string]ContentEntry, len(yFile.Content))
for id, entry := range yFile.Content {
ct.content[id] = NewContentEntry(id, entry)
}
ct.presets = make(map[string]ContentEntry, len(yFile.Presets))
for id, entry := range yFile.Presets {
ct.presets[id] = NewContentEntry(id, entry)
}
// TODO temporary workaround until presets are working properly
// TODO remove
if defPreset, exists := ct.presets["default"]; exists {
for id, entry := range ct.content {
entry.AddMissingValuesFrom(&defPreset)
ct.content[id] = entry
}
}
return ct, nil
}
// ID returns the ID of the chronicle template
func (ct *ChronicleTemplate) ID() string {
return ct.id
}
// Description returns the description of the chronicle template
func (ct *ChronicleTemplate) Description() string {
return ct.description
}
// Inherit returns the ID of the template from which this template inherits
func (ct *ChronicleTemplate) Inherit() string {
return ct.inherit
}
// Filename returns the file name of the chronicle template
func (ct *ChronicleTemplate) Filename() string {
return ct.yFilename
}
// GetPreset returns the preset ContentEntry matching the provided id from
// the current ChronicleTemplate
func (ct *ChronicleTemplate) GetPreset(id string) (ce ContentEntry, exists bool) {
ce, exists = ct.presets[id]
return
}
// GetContent returns the ContentEntry object matching the provided id
// from the current ChronicleTemplate
func (ct *ChronicleTemplate) GetContent(id string) (ce ContentEntry, exists bool) {
ce, exists = ct.content[id]
return
}
// GetContentIDs returns a sorted list of content IDs contained in this chronicle template
func (ct *ChronicleTemplate) GetContentIDs(includeAliases bool) (idList []string) {
idList = make([]string, 0, len(ct.content))
for id, entry := range ct.content {
if includeAliases || id == entry.ID() {
idList = append(idList, id)
}
}
sort.Strings(idList)
return idList
}
// Describe describes a single chronicle template. It returns the
// description as a multi-line string
func (ct *ChronicleTemplate) Describe(verbose bool) (result string) {
var sb strings.Builder
if !verbose {
fmt.Fprintf(&sb, "- %v", ct.ID())
if IsSet(ct.Description()) {
fmt.Fprintf(&sb, ": %v", ct.Description())
}
} else {
fmt.Fprintf(&sb, "- %v\n", ct.ID())
fmt.Fprintf(&sb, "\tDesc: %v\n", ct.Description())
fmt.Fprintf(&sb, "\tFile: %v", ct.Filename())
}
return sb.String()
}