This repository has been archived by the owner on Nov 23, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
electron.go
119 lines (102 loc) · 3.23 KB
/
electron.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
// 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 (
"encoding/base64"
"encoding/gob"
"encoding/json"
"strings"
"time"
)
func init() {
gob.Register(Electron{})
}
// Electron is the base electron that MUST parse from the payload
// from the conductor
type Electron struct {
// SenderID is the unique identifier for the node that sent the
// electron
SenderID string
// ID is the unique identifier of this electron
ID string
// AtomID is the identifier of the atom for this electron instance
// this is generally `package.Type`. Use the atomizer.ID() method
// if unsure of the type for an Atom.
AtomID string
// Timeout is the maximum time duration that should be allowed
// for this instance to process. After the duration is exceeded
// the context should be canceled and the processing released
// and a failure sent back to the conductor
Timeout *time.Duration
// CopyState lets atomizer know if it should copy the state of the
// original atom registration to the new atom instance when processing
// a newly received electron
//
// NOTE: Copying the state of an Atom as registered requires that ALL
// fields that are to be copied are **EXPORTED** otherwise they are
// skipped
CopyState bool
// Payload is to be used by the registered atom to properly unmarshal
// the []byte for the actual atom instance. RawMessage is used to
// delay unmarshal of the payload information so the atom can do it
// internally
Payload []byte
}
// UnmarshalJSON reads in a []byte of JSON data and maps it to the Electron
// struct properly for use throughout Atomizer
func (e *Electron) UnmarshalJSON(data []byte) error {
jsonE := struct {
SenderID string `json:"senderid"`
ID string `json:"id"`
AtomID string `json:"atomid"`
Timeout *time.Duration `json:"timeout,omitempty"`
CopyState bool `json:"copystate,omitempty"`
Payload json.RawMessage `json:"payload,omitempty"`
}{}
err := json.Unmarshal(data, &jsonE)
if err != nil {
return err
}
e.SenderID = jsonE.SenderID
e.ID = jsonE.ID
e.AtomID = jsonE.AtomID
e.Timeout = jsonE.Timeout
if jsonE.Payload != nil {
pay := strings.Trim(string(jsonE.Payload), "\"")
e.Payload, err = base64.StdEncoding.DecodeString(pay)
if err != nil {
e.Payload = jsonE.Payload
}
}
return nil
}
// MarshalJSON implements the custom json marshaler for electron
func (e *Electron) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
SenderID string `json:"senderid"`
ID string `json:"id"`
AtomID string `json:"atomid"`
Timeout *time.Duration `json:"timeout,omitempty"`
CopyState bool `json:"copystate,omitempty"`
Payload json.RawMessage `json:"payload,omitempty"`
}{
SenderID: e.SenderID,
ID: e.ID,
AtomID: e.AtomID,
Timeout: e.Timeout,
Payload: json.RawMessage(e.Payload),
})
}
// Validate ensures that the electron information is intact for proper
// execution
func (e *Electron) Validate() (valid bool) {
if e != nil &&
e.SenderID != "" &&
e.ID != "" &&
e.AtomID != "" {
valid = true
}
return valid
}