Skip to content

Commit

Permalink
Add YAML
Browse files Browse the repository at this point in the history
  • Loading branch information
jkroepke committed May 27, 2024
1 parent 338569d commit 99b7267
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions go/map_cast/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/json-iterator/go v1.1.12
github.com/stretchr/testify v1.9.0
github.com/trivago/tgo v1.0.7
gopkg.in/yaml.v2 v2.4.0
)

require (
Expand Down
73 changes: 73 additions & 0 deletions go/map_cast/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,61 @@ import (
jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/require"
"github.com/trivago/tgo/tcontainer"
yamlv2 "gopkg.in/yaml.v2"
yamlv3 "gopkg.in/yaml.v3"
)

type jira struct {
Fields map[string]any `yaml:"fields"`
}

var (
expectedYAML = []byte(`---
fields:
customfield_10001:
value: green
child:
value: blue
customfield_10002: '2011-10-03'
customfield_10003: '2011-10-19T10:29:29.908+1100'
customfield_10004: Free text goes here. Type away!
customfield_10005:
name: jira-developers
customfield_10007:
- name: admins
- name: jira-developers
- name: jira-users
customfield_10008:
- value: red
- value: blue
- value: green
customfield_10009:
- name: charlie
- name: bjones
- name: tdurden
customfield_10010: 42.07
customfield_10011:
key: JRADEV
customfield_10012:
value: red
customfield_10013:
value: red
customfield_10014:
name: '5.0'
customfield_10015: Is anything better than text?
customfield_10016: http://www.atlassian.com
customfield_10017:
name: brollins
customfield_10018:
- name: '1.0'
- name: 1.1.1
- name: '2.0'
customfield_10019:
- 24
timetracking:
originalEstimate: 1d 2h
remainingEstimate: 3h 25m
`)
expectedJson = []byte(`{"customfield_10001":{"value":"green", "child":{"value":"blue"} },"customfield_10002":"2011-10-03","customfield_10003":"2011-10-19T10:29:29.908+1100","customfield_10004":"Free text goes here. Type away!","customfield_10005":{ "name":"jira-developers" },"customfield_10007":[{ "name":"admins" }, { "name":"jira-developers" }, { "name":"jira-users" }],"customfield_10008":[ {"value":"red" }, {"value":"blue" }, {"value":"green" }],"customfield_10009":[ {"name":"charlie" }, {"name":"bjones" }, {"name":"tdurden" }],"customfield_10010":42.07,"customfield_10011":{ "key":"JRADEV" },"customfield_10012":{ "value":"red" },"customfield_10013":{ "value":"red" },"customfield_10014":{ "name":"5.0" },"customfield_10015":"Is anything better than text?","customfield_10016":"http://www.atlassian.com","customfield_10017":{ "name":"brollins" },"customfield_10018":[{ "name":"1.0" }, { "name":"1.1.1" }, { "name":"2.0" }],"customfield_10019":[24],"timetracking":{"originalEstimate":"1d 2h","remainingEstimate":"3h 25m"}}`)
jiraFieldMap = map[string]any{
"customfield_10001": map[any]any{
Expand Down Expand Up @@ -65,7 +117,28 @@ var (

func TestJsonStdlib(t *testing.T) {
fieldJSON, err := json.Marshal(jiraFieldMap)
require.NoError(t, err)
require.JSONEq(t, string(expectedJson), string(fieldJSON))
}

func TestJsonYamlV2(t *testing.T) {
var jiraFields jira

err := yamlv2.Unmarshal(expectedYAML, &jiraFields)
require.NoError(t, err)

fieldJSON, err := json.Marshal(jiraFields.Fields)
require.NoError(t, err)
require.JSONEq(t, string(expectedJson), string(fieldJSON))
}

func TestJsonYamlV3(t *testing.T) {
var jiraFields jira

err := yamlv3.Unmarshal(expectedYAML, &jiraFields)
require.NoError(t, err)

fieldJSON, err := json.Marshal(jiraFields.Fields)
require.NoError(t, err)
require.JSONEq(t, string(expectedJson), string(fieldJSON))
}
Expand Down

0 comments on commit 99b7267

Please sign in to comment.