This repository has been archived by the owner on Sep 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
payload.go
104 lines (86 loc) · 2.61 KB
/
payload.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
package api
import (
"encoding/json"
"github.com/pkg/errors"
)
// Payload is a basic structure to encapsulate a generic structure.
type Payload struct {
Type string `json:"type"`
Props json.RawMessage `json:"props,omitempty"`
}
// Unmarshal basic payload structure.
func (payload *Payload) Unmarshal(raw []byte) error {
return json.Unmarshal(raw, payload)
}
// MarshalPayloadEntity basic payload structure with an entity.
func MarshalPayloadEntity(typeName string, entity Entity) Payload {
props, _ := json.Marshal(entity)
return Payload{
Type: typeName,
Props: props,
}
}
// Entity returns the appropriate entity as an interface
// based on its type.
func (payload Payload) Entity() (Entity, error) {
if payload.Type == "" {
return nil, errors.New("Empty payload")
}
entityfunc, ok := transform[payload.Type]
if !ok {
if payload.Type == "metadata" {
return nil, errors.New("Cannot create entity from metadata section")
}
return nil, errors.New("Could not determine a suitable type")
}
entity, _ := entityfunc()
if entity == nil {
return nil, errors.New("Could not determine a suitable type")
}
// If there is a payload present we want to try and unmarshal it
if payload.Props != nil {
if err := entity.Unmarshal(payload.Props); err != nil {
return entity, err
}
}
return entity, nil
}
// SafeEntity returns the appropriate entity as an interface
// based on its type and is at the section level.
func (payload Payload) SafeEntity() (Entity, error) {
if payload.Type == "" {
return nil, errors.New("Empty payload")
}
entity, safe := transform[payload.Type]()
if entity == nil || !safe {
return nil, errors.New("Could not determine a suitable type")
}
// If there is a payload present we want to try and unmarshal it
if payload.Props != nil {
if err := entity.Unmarshal(payload.Props); err != nil {
return entity, err
}
}
return entity, nil
}
// UnmarshalEntity returns the appropriate entity as an interface
// based on its type.
func (payload Payload) UnmarshalEntity(raw []byte) (Entity, error) {
// Deserialize the initial payload from a JSON structure
if err := payload.Unmarshal(raw); err != nil {
return nil, err
}
// Extract the entity interface of the payload and validate it
return payload.Entity()
}
// Valid return whether the entity validates.
func (payload Payload) Valid() (bool, error) {
entity, err := payload.Entity()
if err != nil {
return false, err
}
return entity.Valid()
}
// PayloadProperties is a structure of JSON where it is an object
// of named properties which each value being that of a Payload.
// type PayloadProperties map[string]Payload