-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.go
More file actions
155 lines (118 loc) · 3.48 KB
/
types.go
File metadata and controls
155 lines (118 loc) · 3.48 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package icingadsl
import (
"fmt"
"strconv"
"strings"
"time"
)
// indentation holds the current indentation level for String output.
// It is currently used to track the indentation of subelements, example:
// CheckCommand holds one or more CheckCommandArguments that need to be indented
// depending on their CheckCommand.
var indentation int
type Identifier string
// https://icinga.com/docs/icinga-2/latest/doc/17-language-reference/#duration-literals
type Duration struct {
time.Duration
}
// https://icinga.com/docs/icinga-2/latest/doc/18-library-reference/#number-type
// Only Integers for now
type Integer int
type Float float64
// https://icinga.com/docs/icinga-2/latest/doc/18-library-reference/#string-type
type String string
// https://icinga.com/docs/icinga-2/latest/doc/18-library-reference/#dictionary-type
type Dictionary map[Identifier]Object
// https://icinga.com/docs/icinga-2/latest/doc/18-library-reference/#object-type
type Array []Object
// https://icinga.com/docs/icinga-2/latest/doc/18-library-reference/#boolean-type
type Boolean bool
// https://icinga.com/docs/icinga-2/latest/doc/18-library-reference/#object-type
type Object interface {
String() string
}
// https://icinga.com/docs/icinga-2/latest/doc/18-library-reference/#type-type
// TODO Not yet implemented
// https://icinga.com/docs/icinga-2/latest/doc/18-library-reference/#datetime-type
// TODO Not yet implemented
const (
True = Boolean(true)
False = Boolean(false)
)
type Operator uint
const (
PLUS Operator = iota
)
type InfixExpression struct {
Left Object
InfixOperator Operator
Right Object
}
func (i Integer) String() string {
return fmt.Sprintf("%d", i)
}
func (f Float) String() string {
return fmt.Sprintf("%g", f)
}
// Wrapper to stringify the Icinga2 String Object
// Handles escaping and multiline strings when necessary
func (is String) String() string {
if !strings.Contains(string(is), "\n") {
// TODO Escape special characters properly
return strconv.Quote(string(is))
}
if strings.Contains(string(is), "{{{") || strings.Contains(string(is), "}}}") {
panic("Cannot properly escape string")
}
return `{{{` + string(is) + `}}}`
}
// RawString returns the String Object as string without escaping
func (is String) RawString() string {
return string(is)
}
// String returns the Identifier Object as string
func (i Identifier) String() string {
return string(i)
}
// String returns the Array Object as string.
// Uses [] as array markers and , as element delimiter
func (ia *Array) String() string {
var b strings.Builder
b.WriteString("[")
if len(*ia) > 1 {
for i := range len(*ia) - 1 {
b.WriteString((*ia)[i].String() + ", ")
}
b.WriteString((*ia)[len(*ia)-1].String())
} else if len(*ia) == 1 {
b.WriteString((*ia)[0].String())
}
b.WriteString("]")
return b.String()
}
// indentString is a helper function that returns tab indentation
func indentString() string {
return strings.Repeat("\t", indentation)
}
// String returns the Operator Object as string
func (op *Operator) String() string {
if *op == PLUS {
return "+"
}
return ""
}
// String returns the InfixExpression Object as string
func (ie InfixExpression) String() string {
var result strings.Builder
result.WriteString(ie.Left.String() + " ")
result.WriteString(ie.InfixOperator.String() + " ")
result.WriteString((ie.Right).String())
return result.String()
}
// String returns the Boolean Object as string
func (b Boolean) String() string {
if b {
return "true"
}
return "false"
}