Skip to content

Commit a3735f8

Browse files
committed
Checkpoint
1 parent 15c5761 commit a3735f8

File tree

8 files changed

+274
-129
lines changed

8 files changed

+274
-129
lines changed

external/autofill/autofill.go

Lines changed: 8 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,47 @@
11
package autofill
22

33
import (
4-
"fmt"
5-
"reflect"
6-
"strconv"
74
"strings"
85

9-
"github.com/brianvoe/gofakeit/v6"
6+
"github.com/elasticpath/epcc-cli/external/faker"
107
"github.com/elasticpath/epcc-cli/external/resources"
11-
log "github.com/sirupsen/logrus"
128
)
139

14-
var faker = gofakeit.New(0)
15-
16-
var zeroValue = reflect.Value{}
17-
1810
func GetAutoFillQueryParameters(resourceName string, qps []resources.QueryParameter) []string {
1911
args := make([]string, 0)
2012

2113
for _, v := range qps {
2214
key := v.Name
2315
autoFill := v.AutoFill
24-
args = processAutoFill(resourceName, autoFill, args, key, key)
16+
args = processAutoFill(autoFill, args, key)
2517
}
2618

2719
return args
2820
}
21+
2922
func GetJsonArrayForResource(r *resources.Resource) []string {
3023

3124
args := make([]string, 0)
3225

33-
for attributeName, data := range r.Attributes {
26+
for _, data := range r.Attributes {
3427
key := data.Key
3528
key = strings.Replace(key, "data[n]", "data[0]", 1)
3629
autofill := data.AutoFill
3730

38-
args = processAutoFill(r.SingularName, autofill, args, key, attributeName)
31+
args = processAutoFill(autofill, args, key)
3932

4033
}
4134
return args
4235

4336
}
4437

45-
func processAutoFill(resourceName string, autofill string, args []string, key string, attributeName string) []string {
38+
func processAutoFill(autofill string, args []string, key string) []string {
4639
if strings.HasPrefix(autofill, "FUNC:") {
4740

48-
v := reflect.ValueOf(faker)
4941
methodName := strings.Trim(autofill[5:], " ")
5042

51-
var rejectZero = false
52-
if strings.HasPrefix(methodName, "NonZero") {
53-
methodName = methodName[7:]
54-
rejectZero = true
55-
}
56-
57-
method := v.MethodByName(methodName)
58-
if method.IsValid() {
59-
result := method.Call([]reflect.Value{})
60-
if len(result) == 1 {
61-
62-
var arg string
63-
64-
switch {
65-
case result[0].CanUint():
66-
for true {
67-
v := result[0].Uint()
68-
if v == 0 && rejectZero {
69-
result = method.Call([]reflect.Value{})
70-
continue
71-
}
72-
arg = strconv.FormatUint(v, 10)
73-
break
74-
}
75-
76-
case result[0].CanInt():
77-
for true {
78-
v := result[0].Int()
79-
if v == 0 && rejectZero {
80-
result = method.Call([]reflect.Value{})
81-
continue
82-
}
83-
arg = strconv.FormatInt(v, 10)
84-
break
85-
}
86-
default:
87-
arg = result[0].String()
88-
89-
if _, err := strconv.Atoi(arg); err == nil {
90-
// If we get an integer value back, lets just quote it.
91-
arg = fmt.Sprintf("\"%s\"", arg)
92-
}
93-
}
94-
95-
args = append(args, key, arg)
96-
} else {
97-
log.Warnf("Got unexpected number of results from calling %s -> %d", methodName, len(result))
98-
}
99-
100-
} else {
101-
log.Warnf("Could not find autofill method %s for attribute %s on resource %s", methodName, attributeName, resourceName)
102-
}
43+
v := faker.CallFakeFunc(methodName)
44+
args = append(args, key, v)
10345

10446
} else if strings.HasPrefix(autofill, "VALUE:") {
10547
args = append(args, key, strings.Trim(autofill[6:], " "))

external/faker/faker.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package faker
2+
3+
import (
4+
"fmt"
5+
"reflect"
6+
"strconv"
7+
"strings"
8+
9+
"github.com/brianvoe/gofakeit/v6"
10+
log "github.com/sirupsen/logrus"
11+
)
12+
13+
var faker = gofakeit.New(1)
14+
15+
func Seed(n int64) {
16+
faker = gofakeit.New(n)
17+
}
18+
19+
func CallFakeFunc(methodName string) string {
20+
21+
var arg string
22+
v := reflect.ValueOf(faker)
23+
24+
var rejectZero = false
25+
if strings.HasPrefix(methodName, "NonZero") {
26+
methodName = methodName[7:]
27+
rejectZero = true
28+
}
29+
30+
method := v.MethodByName(methodName)
31+
if method.IsValid() {
32+
result := method.Call([]reflect.Value{})
33+
if len(result) == 1 {
34+
35+
switch {
36+
case result[0].CanUint():
37+
for {
38+
v := result[0].Uint()
39+
if v == 0 && rejectZero {
40+
result = method.Call([]reflect.Value{})
41+
continue
42+
}
43+
arg = strconv.FormatUint(v, 10)
44+
break
45+
}
46+
47+
case result[0].CanInt():
48+
for {
49+
v := result[0].Int()
50+
if v == 0 && rejectZero {
51+
result = method.Call([]reflect.Value{})
52+
continue
53+
}
54+
arg = strconv.FormatInt(v, 10)
55+
break
56+
}
57+
default:
58+
arg = result[0].String()
59+
60+
if _, err := strconv.Atoi(arg); err == nil {
61+
// If we get an integer value back, lets just quote it.
62+
arg = fmt.Sprintf("\"%s\"", arg)
63+
}
64+
}
65+
66+
} else {
67+
log.Warnf("Got unexpected number of results from calling %s -> %d", methodName, len(result))
68+
}
69+
70+
} else {
71+
log.Warnf("Could not find autofill method %s", methodName)
72+
}
73+
74+
return arg
75+
}

external/json/to_json.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
gojson "encoding/json"
55
"fmt"
66
"regexp"
7+
"strconv"
78
"strings"
89

910
"github.com/elasticpath/epcc-cli/external/aliases"
@@ -21,7 +22,26 @@ var attributeWithArrayIndex = regexp.MustCompile("\\[[0-9]+]")
2122
func ToJson(args []string, noWrapping bool, compliant bool, attributes map[string]*resources.CrudEntityAttribute, useAliases bool, autoAddConstantValues bool) (string, error) {
2223

2324
if len(args)%2 == 1 {
24-
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\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\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: Enable log level debug to see a dump", len(args))
2545
}
2646

2747
firstArrayKeyIdx := -1

external/json/to_json_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,3 +742,18 @@ func TestToJsonDoesNotAddsAdjacentConstantValuesWithoutAnAdjacentObject(t *testi
742742
t.Fatalf("Testing json conversion of empty value %s did not match\nExpected: %s\nActually: %s", input, expected, actual)
743743
}
744744
}
745+
746+
// This is primarily a "hack" for manual-orders, could be improved later.
747+
func TestToJsonLegacyFormatIncludedKeyNotPromoted(t *testing.T) {
748+
// Fixture Setup
749+
input := []string{"key", "val", "included", "val"}
750+
expected := `{"data":{"key":"val"},"included":"val"}`
751+
752+
// Execute SUT
753+
actual, _ := ToJson(input, false, false, map[string]*resources.CrudEntityAttribute{}, true, true)
754+
755+
// Verification
756+
if actual != expected {
757+
t.Fatalf("Testing json conversion of empty value %s did not match expected %s, actually: %s", input, expected, actual)
758+
}
759+
}

0 commit comments

Comments
 (0)