-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
properties.go
117 lines (104 loc) · 2.88 KB
/
properties.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
// Copyright © 2019 Developer Network, LLC
//
// This file is subject to the terms and conditions defined in
// file 'LICENSE', which is part of this source code package.
package engine
import (
"bytes"
"encoding/json"
"errors"
"time"
)
// TODO: Set it up so that requests can be made to check the properties of
// a bonded electron / atom at runtime
// Properties is the struct for storing properties information after the
// processing of an atom has completed so that it can be sent to the
// original requestor
type Properties struct {
ElectronID string
AtomID string
Start time.Time
End time.Time
Error error
Result []byte
}
// UnmarshalJSON reads in a []byte of JSON data and maps it to the Properties
// struct properly for use throughout Atomizer
func (p *Properties) UnmarshalJSON(data []byte) error {
jsonP := struct {
ElectronID string `json:"electronId"`
AtomID string `json:"atomId"`
Start time.Time `json:"starttime"`
End time.Time `json:"endtime"`
Error []byte `json:"error,omitempty"`
Result json.RawMessage `json:"result"`
}{}
err := json.Unmarshal(data, &jsonP)
if err != nil {
return err
}
if jsonP.Error != nil {
e := &Error{}
err := json.Unmarshal(jsonP.Error, &e)
if err == nil {
p.Error = e
} else {
p.Error = errors.New(string(jsonP.Error))
}
}
p.ElectronID = jsonP.ElectronID
p.AtomID = jsonP.AtomID
p.Start = jsonP.Start
p.End = jsonP.End
p.Result = []byte(jsonP.Result)
return nil
}
// MarshalJSON implements the custom json marshaler for properties
func (p *Properties) MarshalJSON() ([]byte, error) {
var eString []byte
if p.Error != nil {
_, ok := p.Error.(*Error)
if ok {
var err error
eString, err = json.Marshal(p.Error)
if err != nil {
eString = []byte(p.Error.Error())
}
} else {
eString = []byte(p.Error.Error())
}
}
return json.Marshal(&struct {
ElectronID string `json:"electronId"`
AtomID string `json:"atomId"`
Start time.Time `json:"starttime"`
End time.Time `json:"endtime"`
Error []byte `json:"error,omitempty"`
Result json.RawMessage `json:"result"`
}{
ElectronID: p.ElectronID,
AtomID: p.AtomID,
Start: p.Start,
End: p.End,
Error: eString,
Result: json.RawMessage(p.Result),
})
}
// Equal determines if two properties structs are equal to eachother
// TODO: Should this use reflect.DeepEqual?
func (p *Properties) Equal(p2 *Properties) bool {
var eEquals bool
if p.Error != nil {
if p2.Error != nil {
eEquals = p.Error.Error() == p2.Error.Error()
}
} else if p.Error == nil && p2.Error == nil {
eEquals = true
}
return p.ElectronID == p2.ElectronID &&
p.AtomID == p2.AtomID &&
p.Start.Equal(p2.Start) &&
p.End.Equal(p2.End) &&
bytes.Equal(p.Result, p2.Result) &&
eEquals
}