Skip to content

Commit

Permalink
Resolves #298 - Introduce support for custom items (#299)
Browse files Browse the repository at this point in the history
  • Loading branch information
steve-r-west authored Apr 29, 2023
1 parent f020f61 commit 6eebdea
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 10 deletions.
46 changes: 37 additions & 9 deletions external/autofill/autofill.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,48 @@ func GetJsonArrayForResource(r *resources.Resource) []string {

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

var rejectZero = false
if strings.HasPrefix(methodName, "NonZero") {
methodName = methodName[7:]
rejectZero = true
}

method := v.MethodByName(methodName)
if method.IsValid() {
result := method.Call([]reflect.Value{})
if len(result) == 1 {

arg := result[0].String()
var arg string

switch {
case result[0].CanUint():
for true {
v := result[0].Uint()
if v == 0 && rejectZero {
continue
}
arg = strconv.FormatUint(v, 10)
break
}

case result[0].CanInt():
for true {
v := result[0].Int()
if v == 0 && rejectZero {
continue
}
arg = strconv.FormatInt(v, 10)
break
}
default:
arg = result[0].String()

if _, err := strconv.Atoi(arg); err == nil {
// If we get an integer value back, lets just quote it.
arg = fmt.Sprintf("\"%s\"", v)
}
}

args = append(args, data.Key, arg)
} else {
Expand All @@ -46,14 +82,6 @@ func GetJsonArrayForResource(r *resources.Resource) []string {
}

}

for k, v := range args {
if _, err := strconv.Atoi(v); err == nil {
// If we get an integer value back, lets just quote it.
args[k] = fmt.Sprintf("\"%s\"", v)
}
}

return args

}
38 changes: 37 additions & 1 deletion external/resources/yaml/resources.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,43 @@ cart-product-items:
type: STRING
tax[n].rate:
type: FLOAT

cart-custom-items:
singular-name: "cart-custom-item"
json-api-type: "custom_item"
json-api-format: "legacy"
docs: "https://elasticpath.dev/docs/carts/cart-items/add-custom-item-to-cart"
create-entity:
docs: "https://elasticpath.dev/docs/carts/cart-items/add-custom-item-to-cart"
url: "/v2/carts/{carts}/items"
content-type: application/json
attributes:
sku:
type: STRING
quantity:
type: INT
autofill: FUNC:NonZeroUint8
description:
type: STRING
name:
type: STRING
autofill: FUNC:CarModel
^custom_inputs\.([a-zA-Z0-9_-]+)\.$:
type: STRING
price.amount:
type: INT
autofill: FUNC:Uint16
price.includes_tax:
type: BOOL
tax[n].type:
type: ENUM:tax_item
tax[n].name:
type: STRING
tax[n].juristiction:
type: STRING
tax[n].code:
type: STRING
tax[n].rate:
type: FLOAT
cart-checkout:
singular-name: "cart-checkout"
json-api-type: "checkout"
Expand Down

0 comments on commit 6eebdea

Please sign in to comment.