-
Notifications
You must be signed in to change notification settings - Fork 15
/
types.go
141 lines (126 loc) · 3.38 KB
/
types.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
131
132
133
134
135
136
137
138
139
140
141
/**
* Copyright (c) F5, Inc.
*
* This source code is licensed under the Apache License, Version 2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
package crossplane
import (
"fmt"
"strings"
)
type Payload struct {
Status string `json:"status"`
Errors []PayloadError `json:"errors"`
Config []Config `json:"config"`
}
type PayloadError struct {
File string `json:"file"`
Line *int `json:"line"`
Error error `json:"error"`
Callback interface{} `json:"callback,omitempty"`
}
type Config struct {
File string `json:"file"`
Status string `json:"status"`
Errors []ConfigError `json:"errors"`
Parsed Directives `json:"parsed"`
}
type ConfigError struct {
Line *int `json:"line"`
Error error `json:"error"`
}
type Directive struct {
Directive string `json:"directive"`
Line int `json:"line"`
Args []string `json:"args"`
File string `json:"file,omitempty"`
Includes []int `json:"includes,omitempty"`
Block Directives `json:"block,omitempty"`
Comment *string `json:"comment,omitempty"`
}
type Directives []*Directive
// IsBlock returns true if this is a block directive.
func (d Directive) IsBlock() bool {
return d.Block != nil
}
// IsInclude returns true if this is an include directive.
func (d Directive) IsInclude() bool {
return d.Directive == "include" && d.Includes != nil
}
// IsComment returns true iff the directive represents a comment.
func (d Directive) IsComment() bool {
return d.Directive == "#" && d.Comment != nil
}
func equals(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i, x := range a {
if x != b[i] {
return false
}
}
return true
}
// strPtrEqual returns true if the content of the provided string pointer are equal.
func strPtrEqual(a, b *string) bool {
if a == b {
return true
}
if a == nil || b == nil {
return false
}
return *a == *b
}
// Equal returns true if both blocks are functionally equivalent.
func (d *Directive) Equal(a *Directive) bool {
if d == a {
// same ptr, or both nil
return true
}
if d == nil || a == nil {
return false
}
switch {
case a.Directive != d.Directive:
return false
case !equals(a.Args, d.Args):
return false
case len(a.Block) != len(d.Block):
return false
case len(a.Includes) != len(d.Includes):
return false
case !strPtrEqual(a.Comment, d.Comment):
return false
case a.Line != d.Line:
return false
case a.File != d.File:
return false
}
for i, inc := range a.Includes {
if inc != d.Includes[i] {
return false
}
}
for i, dir := range a.Block {
if !dir.Equal(d.Block[i]) {
return false
}
}
return true
}
// String makes this a Stringer, returning a string representation of the Directive. The string representation is a
// peak at the content of the Directive, does not represent a valid config rendering of the Directive in question.
func (d *Directive) String() string {
if len(d.Block) == 0 {
return fmt.Sprintf("%s %s", d.Directive, strings.Join(d.Args, " "))
}
return fmt.Sprintf("%s %s {...}", d.Directive, strings.Join(d.Args, " "))
}
// Combined returns a new Payload that is the same except that the inluding
// logic is performed on its configs. This means that the resulting Payload
// will always have 0 or 1 configs in its Config field.
func (p *Payload) Combined() (*Payload, error) {
return combineConfigs(p)
}