Skip to content

Commit cf26c8b

Browse files
author
Frank Martinez
committed
fix metadata marshal/unmarshal
1 parent b084a45 commit cf26c8b

4 files changed

Lines changed: 111 additions & 34 deletions

File tree

action/metadata.go

Lines changed: 59 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,40 +12,71 @@ type Metadata struct {
1212
Settings map[string]data.TypedValue
1313
}
1414

15-
// UnmarshalJSON overrides the default UnmarshalJSON for TaskEnv
16-
func (md *Metadata) UnmarshalJSON(b []byte) error {
17-
18-
ser := &struct {
19-
Settings []*data.Attribute `json:"settings"`
20-
Input []*data.Attribute `json:"input"`
21-
Output []*data.Attribute `json:"output"`
22-
}{}
23-
24-
if err := json.Unmarshal(b, ser); err != nil {
25-
return err
26-
}
27-
28-
md.IOMetadata = &metadata.IOMetadata{}
29-
30-
md.Settings = make(map[string]data.TypedValue, len(ser.Settings))
31-
md.Input = make(map[string]data.TypedValue, len(ser.Input))
32-
md.Output = make(map[string]data.TypedValue, len(ser.Output))
33-
34-
for _, attr := range ser.Settings {
35-
md.Settings[attr.Name()] = attr
15+
func (md *Metadata) MarshalJSON() ([]byte, error) {
16+
var mdSettings []*data.Attribute
17+
var mdInputs []*data.Attribute
18+
var mdOutputs []*data.Attribute
19+
20+
for _, v := range md.Settings {
21+
if attr,ok := v.(*data.Attribute); ok {
22+
mdSettings = append(mdSettings, attr)
23+
}
3624
}
37-
38-
for _, attr := range ser.Input {
39-
md.Input[attr.Name()] = attr
25+
for _, v := range md.Input {
26+
if attr,ok := v.(*data.Attribute); ok {
27+
mdInputs = append(mdInputs, attr)
28+
}
4029
}
41-
42-
for _, attr := range ser.Output {
43-
md.Output[attr.Name()] = attr
30+
for _, v := range md.Output {
31+
if attr,ok := v.(*data.Attribute); ok {
32+
mdOutputs = append(mdOutputs, attr)
33+
}
4434
}
4535

46-
return nil
36+
return json.Marshal(&struct {
37+
Settings []*data.Attribute `json:"settings,omitempty"`
38+
Input []*data.Attribute `json:"input,omitempty"`
39+
Output []*data.Attribute `json:"output,omitempty"`
40+
}{
41+
Settings:mdSettings,
42+
Input: mdInputs,
43+
Output: mdOutputs,
44+
})
4745
}
4846

47+
//func (md *Metadata) UnmarshalJSON(b []byte) error {
48+
//
49+
// ser := &struct {
50+
// Settings []*data.Attribute `json:"settings"`
51+
// Input []*data.Attribute `json:"input"`
52+
// Output []*data.Attribute `json:"output"`
53+
// }{}
54+
//
55+
// if err := json.Unmarshal(b, ser); err != nil {
56+
// return err
57+
// }
58+
//
59+
// md.IOMetadata = &metadata.IOMetadata{}
60+
//
61+
// md.Settings = make(map[string]data.TypedValue, len(ser.Settings))
62+
// md.Output = make(map[string]data.TypedValue, len(ser.Output))
63+
// md.Output = make(map[string]data.TypedValue, len(ser.Output))
64+
//
65+
// for _, attr := range ser.Settings {
66+
// md.Settings[attr.Name()] = attr
67+
// }
68+
//
69+
// for _, attr := range ser.Input {
70+
// md.Input[attr.Name()] = attr
71+
// }
72+
//
73+
// for _, attr := range ser.Output {
74+
// md.Output[attr.Name()] = attr
75+
// }
76+
//
77+
// return nil
78+
//}
79+
4980
func ToMetadata(mdStructs ...interface{}) *Metadata {
5081

5182
var settings map[string]data.TypedValue

app/config.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ type Config struct {
1515
Version string `json:"version"`
1616
Description string `json:"description"`
1717

18-
Imports []string `json:"imports"`
19-
Properties []*data.Attribute `json:"properties"`
20-
Channels []string `json:"channels"`
18+
Imports []string `json:"imports,omitempty"`
19+
Properties []*data.Attribute `json:"properties,omitempty"`
20+
Channels []string `json:"channels,omitempty"`
2121
Triggers []*trigger.Config `json:"triggers"`
2222
Resources []*resource.Config `json:"resources"`
23-
Actions []*action.Config `json:"actions"`
23+
Actions []*action.Config `json:"actions,omitempty"`
2424
Schemas map[string]*schema.Def `json:"schemas,omitempty"`
2525
}

data/attribute.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package data
22

33
import (
44
"encoding/json"
5+
56
"github.com/project-flogo/core/data/schema"
67
)
78

@@ -49,15 +50,35 @@ func (a *Attribute) Schema() schema.Schema {
4950
return a.schema
5051
}
5152

52-
//todo temporary work around for attribute deserialization
53+
func (a *Attribute) MarshalJSON() ([]byte, error) {
54+
55+
val := a.value
56+
if vs, ok := val.(string); ok {
57+
if vs == "" {
58+
val = nil //hack to omit empty string value
59+
}
60+
}
61+
62+
return json.Marshal(&struct {
63+
Name string `json:"name"`
64+
Type string `json:"type"`
65+
Value interface{} `json:"value,omitempty"`
66+
Schema interface{} `json:"schema,omitempty"`
67+
}{
68+
Name: a.name,
69+
Type: a.dataType.String(),
70+
Value: val,
71+
Schema: a.schema,
72+
})
73+
}
5374

5475
// UnmarshalJSON implements json.Unmarshaler.UnmarshalJSON
5576
func (a *Attribute) UnmarshalJSON(data []byte) error {
5677

5778
ser := &struct {
5879
Name string `json:"name"`
5980
Type string `json:"type"`
60-
Value interface{} `json:"value"`
81+
Value interface{} `json:"value,omitempty"`
6182
Schema interface{} `json:"schema,omitempty"`
6283

6384
//KeyType string `json:"keyType,omitempty"`

data/metadata/metadata.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,31 @@ type IOMetadata struct {
5151
Output map[string]data.TypedValue
5252
}
5353

54+
func (md *IOMetadata) MarshalJSON() ([]byte, error) {
55+
var mdInputs []*data.Attribute
56+
var mdOutputs []*data.Attribute
57+
58+
for _, v := range md.Input {
59+
if attr,ok := v.(*data.Attribute); ok {
60+
mdInputs = append(mdInputs, attr)
61+
}
62+
}
63+
for _, v := range md.Output {
64+
if attr,ok := v.(*data.Attribute); ok {
65+
mdOutputs = append(mdOutputs, attr)
66+
}
67+
}
68+
69+
return json.Marshal(&struct {
70+
Input []*data.Attribute `json:"input,omitempty"`
71+
Output []*data.Attribute `json:"output,omitempty"`
72+
}{
73+
Input: mdInputs,
74+
Output: mdOutputs,
75+
})
76+
}
77+
78+
5479
func (md *IOMetadata) UnmarshalJSON(b []byte) error {
5580

5681
ser := &struct {

0 commit comments

Comments
 (0)