-
Notifications
You must be signed in to change notification settings - Fork 54
/
spec.go
40 lines (35 loc) · 1.02 KB
/
spec.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
package kazaam
import (
"encoding/json"
"github.com/qntfy/kazaam/v4/transform"
)
// Spec represents an individual spec element. It describes the name of the operation,
// whether the `over` operator is required, and an operation-specific `Config` that
// describes the configuration of the transform.
type spec struct {
*transform.Config
Operation *string `json:"operation"`
Over *string `json:"over,omitempty"`
}
type specInt spec
type specs []spec
// UnmarshalJSON implements a custon unmarshaller for the Spec type
func (s *spec) UnmarshalJSON(b []byte) (err error) {
j := specInt{}
if err = json.Unmarshal(b, &j); err == nil {
*s = spec(j)
if s.Operation == nil {
err = &Error{ErrMsg: "Spec must contain an \"operation\" field", ErrType: SpecError}
return
}
if s.Config != nil && s.Spec != nil && len(*s.Spec) < 1 {
err = &Error{ErrMsg: "Spec must contain at least one element", ErrType: SpecError}
return
}
if s.Config != nil && s.KeySeparator == "" {
s.KeySeparator = "."
}
return
}
return
}