Skip to content

Commit 3f82d00

Browse files
authored
feat: improve debugging for unmatched parameters and support top level included (Resolves #587) (#588)
1 parent 1537c24 commit 3f82d00

File tree

4 files changed

+67
-10
lines changed

4 files changed

+67
-10
lines changed

external/json/to_json.go

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ package json
33
import (
44
gojson "encoding/json"
55
"fmt"
6+
"regexp"
7+
"strconv"
8+
"strings"
9+
610
"github.com/elasticpath/epcc-cli/external/aliases"
711
"github.com/elasticpath/epcc-cli/external/resources"
812
"github.com/elasticpath/epcc-cli/external/templates"
913
"github.com/itchyny/gojq"
1014
"github.com/mitchellh/mapstructure"
1115
log "github.com/sirupsen/logrus"
12-
"regexp"
13-
"strings"
1416
)
1517

1618
var segmentRegex = regexp.MustCompile("(.+?)(\\[[0-9]+])?$")
@@ -20,7 +22,26 @@ var attributeWithArrayIndex = regexp.MustCompile("\\[[0-9]+]")
2022
func ToJson(args []string, noWrapping bool, compliant bool, attributes map[string]*resources.CrudEntityAttribute, useAliases bool, autoAddConstantValues bool) (string, error) {
2123

2224
if len(args)%2 == 1 {
23-
return "", fmt.Errorf("the number of arguments %d supplied isn't even, json should be passed in key value pairs. Do you have an extra/missing id?", len(args))
25+
if log.IsLevelEnabled(log.DebugLevel) {
26+
fmt.Printf("\n\nArgument Dump:\n")
27+
maxlen := 0
28+
29+
for _, v := range args {
30+
if len(v) > maxlen {
31+
maxlen = len(v)
32+
}
33+
}
34+
35+
for i := 0; i < len(args); i++ {
36+
fmt.Printf("%"+strconv.Itoa(maxlen)+"s ", args[i])
37+
38+
if i%2 == 1 {
39+
fmt.Println()
40+
}
41+
}
42+
fmt.Printf("\n\n")
43+
}
44+
return "", fmt.Errorf("the number of arguments %d supplied isn't even, json should be passed in key value pairs. Do you have an extra/missing id? **Tip**: Use --log debug to see a column aligned dump", len(args))
2445
}
2546

2647
firstArrayKeyIdx := -1
@@ -183,7 +204,26 @@ func toJsonObject(args []string, noWrapping bool, compliant bool, attributes map
183204
}
184205

185206
if !noWrapping {
186-
result, err = RunJQ(`{ "data": . }`, result)
207+
208+
if rm, ok := result.(map[string]interface{}); ok {
209+
var inc any
210+
if included, ok := rm["included"]; ok {
211+
delete(rm, "included")
212+
inc = included
213+
214+
result, err = RunJQ(`{ "data": . }`, result)
215+
216+
if rm2, ok := result.(map[string]interface{}); ok {
217+
rm2["included"] = inc
218+
}
219+
} else {
220+
result, err = RunJQ(`{ "data": . }`, result)
221+
}
222+
223+
} else {
224+
result, err = RunJQ(`{ "data": . }`, result)
225+
}
226+
187227
}
188228

189229
jsonStr, err := gojson.Marshal(result)

external/json/to_json_test.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package json
22

33
import (
44
"fmt"
5+
"testing"
6+
57
"github.com/elasticpath/epcc-cli/external/aliases"
68
"github.com/elasticpath/epcc-cli/external/resources"
79
"github.com/stretchr/testify/require"
8-
"testing"
910
)
1011

1112
func init() {
@@ -16,7 +17,7 @@ func init() {
1617
func TestErrorMessageWhenOddNumberOfValuesPassed(t *testing.T) {
1718
// Fixture Setup
1819
input := []string{"[0]"}
19-
expected := fmt.Errorf("the number of arguments 1 supplied isn't even, json should be passed in key value pairs. Do you have an extra/missing id?")
20+
expected := fmt.Errorf("the number of arguments 1 supplied isn't even, json should be passed in key value pairs. Do you have an extra/missing id? **Tip**: Use --log debug to see a column aligned dump")
2021

2122
// Execute SUT
2223
_, actual := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}, true, true)
@@ -742,3 +743,18 @@ func TestToJsonDoesNotAddsAdjacentConstantValuesWithoutAnAdjacentObject(t *testi
742743
t.Fatalf("Testing json conversion of empty value %s did not match\nExpected: %s\nActually: %s", input, expected, actual)
743744
}
744745
}
746+
747+
// This is primarily a "hack" for manual-orders, could be improved later.
748+
func TestToJsonLegacyFormatIncludedKeyNotPromoted(t *testing.T) {
749+
// Fixture Setup
750+
input := []string{"key", "val", "included", "val"}
751+
expected := `{"data":{"key":"val"},"included":"val"}`
752+
753+
// Execute SUT
754+
actual, _ := ToJson(input, false, false, map[string]*resources.CrudEntityAttribute{}, true, true)
755+
756+
// Verification
757+
if actual != expected {
758+
t.Fatalf("Testing json conversion of empty value %s did not match expected %s, actually: %s", input, expected, actual)
759+
}
760+
}

external/runbooks/account-cart-associations.epcc.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ actions:
6969
delete-accounts-and-carts:
7070
description:
7171
short: "Delete all the accounts and their carts"
72+
ignore_errors: true
7273
commands:
7374
- epcc get -s accounts
7475
- epcc delete account-cart-association id=acct1_only data[0].type account data[0].id name=acct1

external/runbooks/customer-cart-associations.epcc.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ actions:
5555
epcc create customer --auto-fill email "[email protected]" password test
5656
epcc create customer --auto-fill email "[email protected]" password test
5757
epcc create customer --auto-fill email "[email protected]" password test
58-
epcc create cart name "Customer 4 Cart" id cust1_only description "Customer 4 Cart"
59-
epcc create cart name "Customer 4&5 Shared Cart" id cust1_and_2 description "Customer 4 & 5 Cart"
58+
epcc create cart name "Customer 4 Cart" id cust4_only description "Customer 4 Cart"
59+
epcc create cart name "Customer 4&5 Shared Cart" id cust4_and_5 description "Customer 4 & 5 Cart"
6060
epcc create cart name "No Customer Cart" id no_cust_cart_custom_items description "No Customer Cart (Custom Items)"
6161
- epcc create customer-cart-association id=cust4_only data[0].type customer data[0].id [email protected]
6262
- epcc create customer-cart-association id=cust4_and_5 data[0].type customer data[0].id [email protected]
@@ -69,7 +69,7 @@ actions:
6969
delete-customer-and-carts-with-product-items:
7070
description:
7171
short: "Delete all the customers and their carts"
72-
ignore_errors: false
72+
ignore_errors: true
7373
commands:
7474
- epcc get -s customers filter 'like(email,*@example.com)'
7575
- epcc delete customer-cart-association id=cust1_only data[0].type customer data[0].id [email protected]
@@ -109,7 +109,7 @@ actions:
109109
delete-customer-and-carts-with-custom-items:
110110
description:
111111
short: "Delete all the customers and their carts"
112-
ignore_errors: false
112+
ignore_errors: true
113113
commands:
114114
- epcc get -s customers filter 'like(email,*@example.com)'
115115
- epcc delete customer-cart-association id=cust4_only data[0].type customer data[0].id [email protected]

0 commit comments

Comments
 (0)