This repository has been archived by the owner on Jun 1, 2023. It is now read-only.
/
input_data.go
78 lines (72 loc) · 2.5 KB
/
input_data.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
package graylog
import (
"fmt"
"github.com/suzuki-shunsuke/go-graylog/util"
)
// InputUpdateParamsData represents InputUpdateParams's data.
// This is used for data conversion of InputUpdateParams.
// ex. json.Unmarshal
type InputUpdateParamsData struct {
ID string `json:"id,omitempty"`
Title string `json:"title,omitempty"`
Type string `json:"type,omitempty"`
Node string `json:"node,omitempty"`
Global *bool `json:"global,omitempty"`
Attrs map[string]interface{} `json:"attributes,omitempty"`
}
// InputData represents data of Input.
// This is used for data conversion of Input.
// ex. json.Unmarshal
type InputData struct {
Title string `json:"title,omitempty"`
Type string `json:"type,omitempty"`
ID string `json:"id,omitempty"`
Node string `json:"node,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
CreatorUserID string `json:"creator_user_id,omitempty"`
Global bool `json:"global,omitempty"`
Attrs map[string]interface{} `json:"attributes,omitempty"`
}
// ToInputUpdateParams copies InputUpdateParamsData's data to InputUpdateParams.
func (d *InputUpdateParamsData) ToInputUpdateParams(input *InputUpdateParams) error {
input.Title = d.Title
input.Type = d.Type
input.ID = d.ID
input.Global = d.Global
input.Node = d.Node
attrs := NewInputAttrsByType(input.Type)
if _, ok := attrs.(*InputUnknownAttrs); ok {
input.Attrs = &InputUnknownAttrs{inputType: input.Type, Data: d.Attrs}
return nil
}
if err := util.MSDecode(d.Attrs, attrs); err != nil {
return err
}
input.Attrs = attrs
return nil
}
// ToInput copies InputData's data to Input.
func (d *InputData) ToInput(input *Input) error {
if input.Type() != "" && input.Type() != d.Type {
return fmt.Errorf("input type is different")
}
if input.Attrs != nil && input.Attrs.InputType() != d.Type {
return fmt.Errorf("input type is different")
}
input.Title = d.Title
input.ID = d.ID
input.Global = d.Global
input.Node = d.Node
input.CreatedAt = d.CreatedAt
input.CreatorUserID = d.CreatorUserID
attrs := NewInputAttrsByType(d.Type)
if _, ok := attrs.(*InputUnknownAttrs); ok {
input.Attrs = &InputUnknownAttrs{inputType: input.Type(), Data: d.Attrs}
return nil
}
if err := util.MSDecode(d.Attrs, attrs); err != nil {
return err
}
input.Attrs = attrs
return nil
}