diff --git a/cmd/create.go b/cmd/create.go index 5dd8461..fa3e4e7 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -224,7 +224,7 @@ func createInternal(ctx context.Context, overrides *httpclient.HttpParameterOver } // Replace ids with args in resourceURL - resourceURL, err = resources.GenerateUrl(resource.CreateEntityInfo, args[1:]) + resourceURL, err = resources.GenerateUrl(resource.CreateEntityInfo, args[1:], true) if overrides.OverrideUrlPath != "" { log.Warnf("Overriding URL Path from %s to %s", resourceURL, overrides.OverrideUrlPath) @@ -273,7 +273,7 @@ func createInternal(ctx context.Context, overrides *httpclient.HttpParameterOver jsonArgs = append(autofilledData, jsonArgs...) } - body, err := json.ToJson(jsonArgs, resource.NoWrapping, resource.JsonApiFormat == "compliant", resource.Attributes) + body, err := json.ToJson(jsonArgs, resource.NoWrapping, resource.JsonApiFormat == "compliant", resource.Attributes, true) if err != nil { return "", err diff --git a/cmd/delete.go b/cmd/delete.go index d14dd5c..963dc88 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -218,7 +218,7 @@ func deleteResource(ctx context.Context, overrides *httpclient.HttpParameterOver } // Replace ids with args in resourceURL - resourceURL, err := resources.GenerateUrl(resource.DeleteEntityInfo, args[1:]) + resourceURL, err := resources.GenerateUrl(resource.DeleteEntityInfo, args[1:], true) if err != nil { return nil, err @@ -250,7 +250,7 @@ func deleteResource(ctx context.Context, overrides *httpclient.HttpParameterOver var payload io.Reader = nil if len(jsonArgs) > 0 { - body, err := json.ToJson(jsonArgs, resource.NoWrapping, resource.JsonApiFormat == "compliant", resource.Attributes) + body, err := json.ToJson(jsonArgs, resource.NoWrapping, resource.JsonApiFormat == "compliant", resource.Attributes, true) if err != nil { return nil, err diff --git a/cmd/get.go b/cmd/get.go index f924989..5bf76d0 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -338,7 +338,7 @@ func getResource(ctx context.Context, overrides *httpclient.HttpParameterOverrid } // Replace ids with args in resourceURL - resourceURL, err := resources.GenerateUrl(resourceUrlInfo, args[1:]) + resourceURL, err := resources.GenerateUrl(resourceUrlInfo, args[1:], true) if err != nil { return nil, err diff --git a/cmd/helper.go b/cmd/helper.go index 19cc62a..3f20558 100644 --- a/cmd/helper.go +++ b/cmd/helper.go @@ -212,7 +212,7 @@ func FillUrlWithIds(urlInfo *resources.CrudEntityInfo, uuids []string) string { ids = append(ids, uuids[i]) } - url, err := resources.GenerateUrl(urlInfo, ids) + url, err := resources.GenerateUrl(urlInfo, ids, false) if err != nil { log.Errorf("error generating help screen %v", err) @@ -312,7 +312,7 @@ func toJsonExample(in []string, resource resources.Resource) string { in = append([]string{"type", resource.JsonApiType}, in...) } - jsonTxt, err := json.ToJson(in, resource.NoWrapping, resource.JsonApiFormat == "compliant", resource.Attributes) + jsonTxt, err := json.ToJson(in, resource.NoWrapping, resource.JsonApiFormat == "compliant", resource.Attributes, false) if err != nil { return fmt.Sprintf("Could not get json: %s", err) @@ -627,7 +627,7 @@ func GetCreateExample(resource resources.Resource) string { baseJsonArgs = append(baseJsonArgs, "type", resource.JsonApiType) } - emptyJson, _ := json.ToJson(baseJsonArgs, resource.NoWrapping, resource.JsonApiFormat == "compliant", resource.Attributes) + emptyJson, _ := json.ToJson(baseJsonArgs, resource.NoWrapping, resource.JsonApiFormat == "compliant", resource.Attributes, false) examples := GetJsonExample(fmt.Sprintf("# Create a %s", resource.SingularName), exampleWithIds, fmt.Sprintf("> POST %s", FillUrlWithIds(resource.CreateEntityInfo, uuids)), emptyJson) @@ -648,6 +648,7 @@ func GetCreateExample(resource resources.Resource) string { Verb: completion.Create, Attribute: k, ToComplete: "", + NoAliases: true, }) arg := `"Hello World"` @@ -660,14 +661,14 @@ func GetCreateExample(resource resources.Resource) string { // Don't try and use more than one key as some are mutually exclusive and the JSON will crash. // Resources that are heterogenous and can have array or object fields at some level (i.e., data[n].id and data.id) are examples - jsonTxt, _ := json.ToJson(extendedArgs, resource.NoWrapping, resource.JsonApiFormat == "compliant", resource.Attributes) + jsonTxt, _ := json.ToJson(extendedArgs, resource.NoWrapping, resource.JsonApiFormat == "compliant", resource.Attributes, false) examples += GetJsonExample(fmt.Sprintf("# Create a %s passing in an argument", resourceName), fmt.Sprintf("%s %s %s", exampleWithAliases, k, arg), fmt.Sprintf("> POST %s", FillUrlWithIds(resource.CreateEntityInfo, uuids)), jsonTxt) autofilledData := autofill.GetJsonArrayForResource(&resource) extendedArgs = append(autofilledData, extendedArgs...) - jsonTxt, _ = json.ToJson(extendedArgs, resource.NoWrapping, resource.JsonApiFormat == "compliant", resource.Attributes) + jsonTxt, _ = json.ToJson(extendedArgs, resource.NoWrapping, resource.JsonApiFormat == "compliant", resource.Attributes, false) examples += GetJsonExample(fmt.Sprintf("# Create a %s (using --auto-fill) and passing in an argument", resourceName), fmt.Sprintf("%s --auto-fill %s %s", exampleWithAliases, k, arg), fmt.Sprintf("> POST %s", FillUrlWithIds(resource.CreateEntityInfo, uuids)), jsonTxt) break @@ -704,7 +705,7 @@ func GetUpdateExample(resource resources.Resource) string { baseJsonArgs = append(baseJsonArgs, "type", resource.JsonApiType) } - emptyJson, _ := json.ToJson(baseJsonArgs, resource.NoWrapping, resource.JsonApiFormat == "compliant", resource.Attributes) + emptyJson, _ := json.ToJson(baseJsonArgs, resource.NoWrapping, resource.JsonApiFormat == "compliant", resource.Attributes, false) examples := GetJsonExample(fmt.Sprintf("# Update a %s", resource.SingularName), exampleWithIds, fmt.Sprintf("> PUT %s", FillUrlWithIds(resource.UpdateEntityInfo, uuids)), emptyJson) @@ -725,6 +726,7 @@ func GetUpdateExample(resource resources.Resource) string { Verb: completion.Update, Attribute: k, ToComplete: "", + NoAliases: true, }) arg := `"Hello World"` @@ -737,7 +739,7 @@ func GetUpdateExample(resource resources.Resource) string { // Don't try and use more than one key as some are mutually exclusive and the JSON will crash. // Resources that are heterogenous and can have array or object fields at some level (i.e., data[n].id and data.id) are examples - jsonTxt, _ := json.ToJson(extendedArgs, resource.NoWrapping, resource.JsonApiFormat == "compliant", resource.Attributes) + jsonTxt, _ := json.ToJson(extendedArgs, resource.NoWrapping, resource.JsonApiFormat == "compliant", resource.Attributes, false) examples += GetJsonExample(fmt.Sprintf("# update a %s passing in an argument", resourceName), fmt.Sprintf("%s %s %s", exampleWithAliases, k, arg), fmt.Sprintf("> PUT %s", FillUrlWithIds(resource.UpdateEntityInfo, uuids)), jsonTxt) break @@ -772,7 +774,7 @@ func GetDeleteExample(resource resources.Resource) string { baseJsonArgs = append(baseJsonArgs, "type", resource.JsonApiType) } - emptyJson, _ := json.ToJson(baseJsonArgs, resource.NoWrapping, resource.JsonApiFormat == "compliant", resource.Attributes) + emptyJson, _ := json.ToJson(baseJsonArgs, resource.NoWrapping, resource.JsonApiFormat == "compliant", resource.Attributes, false) examples := GetJsonExample(fmt.Sprintf("# Delete a %s", resource.SingularName), exampleWithIds, fmt.Sprintf("> PUT %s", FillUrlWithIds(resource.DeleteEntityInfo, uuids)), emptyJson) @@ -793,6 +795,7 @@ func GetDeleteExample(resource resources.Resource) string { Verb: completion.Delete, Attribute: k, ToComplete: "", + NoAliases: true, }) arg := `"Hello World"` @@ -805,7 +808,7 @@ func GetDeleteExample(resource resources.Resource) string { // Don't try and use more than one key as some are mutually exclusive and the JSON will crash. // Resources that are heterogenous and can have array or object fields at some level (i.e., data[n].id and data.id) are examples - jsonTxt, _ := json.ToJson(extendedArgs, resource.NoWrapping, resource.JsonApiFormat == "compliant", resource.Attributes) + jsonTxt, _ := json.ToJson(extendedArgs, resource.NoWrapping, resource.JsonApiFormat == "compliant", resource.Attributes, false) examples += GetJsonExample(fmt.Sprintf("# delete a %s passing in an argument", resourceName), fmt.Sprintf("%s %s %s", exampleWithAliases, k, arg), fmt.Sprintf("> DELETE %s", FillUrlWithIds(resource.DeleteEntityInfo, uuids)), jsonTxt) break diff --git a/cmd/reset-store.go b/cmd/reset-store.go index 0dd4117..6e4db44 100644 --- a/cmd/reset-store.go +++ b/cmd/reset-store.go @@ -115,7 +115,7 @@ func getStoreId(ctx context.Context, args []string) (string, error) { return "", fmt.Errorf("could not find resource %s, we need it to determine the store id.", args[0]) } - resourceURL, err := resources.GenerateUrl(resource.GetCollectionInfo, make([]string, 0)) + resourceURL, err := resources.GenerateUrl(resource.GetCollectionInfo, make([]string, 0), true) if err != nil { return "", err diff --git a/cmd/root.go b/cmd/root.go index cdcabb5..0065edd 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -53,9 +53,10 @@ var jqCompletionFunc = func(cmd *cobra.Command, args []string, toComplete string }, cobra.ShellCompDirectiveNoSpace } -var profileNameFromCommandLine = "default" +var profileNameFromCommandLine = "" func InitializeCmd() { + cobra.OnInitialize(initConfig) initConfig() diff --git a/cmd/test-json.go b/cmd/test-json.go index 0589687..da423a1 100644 --- a/cmd/test-json.go +++ b/cmd/test-json.go @@ -12,7 +12,7 @@ var testJson = &cobra.Command{ Use: "test-json [KEY_1] [VAL_1] [KEY_2] [VAL_2] ...", Short: "Prints the resulting json for what a command will look like", RunE: func(cmd *cobra.Command, args []string) error { - res, err := json.ToJson(args, noWrapping, compliant, map[string]*resources.CrudEntityAttribute{}) + res, err := json.ToJson(args, noWrapping, compliant, map[string]*resources.CrudEntityAttribute{}, true) if res != "" { json.PrintJson(res) diff --git a/cmd/update.go b/cmd/update.go index eb24cb3..93a8abc 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -199,7 +199,7 @@ func updateInternal(ctx context.Context, overrides *httpclient.HttpParameterOver } // Replace ids with args in resourceURL - resourceURL, err := resources.GenerateUrl(resourceUrlInfo, args[1:]) + resourceURL, err := resources.GenerateUrl(resourceUrlInfo, args[1:], true) if err != nil { return "", err } @@ -211,7 +211,7 @@ func updateInternal(ctx context.Context, overrides *httpclient.HttpParameterOver args = append(args, "type", resource.JsonApiType) // Create the body from remaining args - body, err := json.ToJson(args[(idCount+1):], resource.NoWrapping, resource.JsonApiFormat == "compliant", resource.Attributes) + body, err := json.ToJson(args[(idCount+1):], resource.NoWrapping, resource.JsonApiFormat == "compliant", resource.Attributes, true) if err != nil { return "", err } diff --git a/external/aliases/aliases.go b/external/aliases/aliases.go index adb9d63..3011392 100644 --- a/external/aliases/aliases.go +++ b/external/aliases/aliases.go @@ -26,7 +26,6 @@ var typeToAliasNameToIdMap = map[string]map[string]*id.IdableAttributes{} var dirtyAliases = map[string]bool{} var SkipAliasProcessing = false - var typeToIdToAliasNamesMap = map[string]map[string]map[string]bool{} func ClearAllAliasesForJsonApiType(jsonApiType string) error { @@ -46,7 +45,6 @@ var arrayPathPattern = regexp.MustCompile("^\\.data\\[([0-9]+)]$") var relationshipPattern = regexp.MustCompile("^\\.data(?:\\[[0-9]+])?\\.relationships\\.([^.]+)\\.data") func GetAliasesForJsonApiTypeAndAlternates(jsonApiType string, alternateJsonApiTypes []string) map[string]*id.IdableAttributes { - aliases := map[string]*id.IdableAttributes{} for k, v := range getAliasesForSingleJsonApiType(jsonApiType) { diff --git a/external/completion/completion.go b/external/completion/completion.go index 46c5ac0..854b602 100644 --- a/external/completion/completion.go +++ b/external/completion/completion.go @@ -42,6 +42,7 @@ type Request struct { Attribute string QueryParam string ToComplete string + NoAliases bool } func Complete(c Request) ([]string, cobra.ShellCompDirective) { @@ -216,29 +217,34 @@ func Complete(c Request) ([]string, cobra.ShellCompDirective) { case 3, 4: if aliasType, ok := resources.GetResourceByName(fullyQualifiedAlias[1]); ok { - for alias := range aliases.GetAliasesForJsonApiTypeAndAlternates(aliasType.JsonApiType, aliasType.AlternateJsonApiTypesForAliases) { - results = append(results, "alias/"+aliasType.JsonApiType+"/"+alias+"/id") + if !c.NoAliases { + for alias := range aliases.GetAliasesForJsonApiTypeAndAlternates(aliasType.JsonApiType, aliasType.AlternateJsonApiTypesForAliases) { + results = append(results, "alias/"+aliasType.JsonApiType+"/"+alias+"/id") - if _, ok2 := aliasType.Attributes["sku"]; ok2 { - results = append(results, "alias/"+aliasType.JsonApiType+"/"+alias+"/sku") - } + if _, ok2 := aliasType.Attributes["sku"]; ok2 { + results = append(results, "alias/"+aliasType.JsonApiType+"/"+alias+"/sku") + } - if _, ok2 := aliasType.Attributes["slug"]; ok2 { - results = append(results, "alias/"+aliasType.JsonApiType+"/"+alias+"/slug") - } + if _, ok2 := aliasType.Attributes["slug"]; ok2 { + results = append(results, "alias/"+aliasType.JsonApiType+"/"+alias+"/slug") + } - if _, ok2 := aliasType.Attributes["code"]; ok2 { - results = append(results, "alias/"+aliasType.JsonApiType+"/"+alias+"/code") - } + if _, ok2 := aliasType.Attributes["code"]; ok2 { + results = append(results, "alias/"+aliasType.JsonApiType+"/"+alias+"/code") + } + } } } } } else if aliasType, ok := resources.GetResourceByName(resourceType); ok { - for alias := range aliases.GetAliasesForJsonApiTypeAndAlternates(aliasType.JsonApiType, aliasType.AlternateJsonApiTypesForAliases) { - results = append(results, alias) + + if !c.NoAliases { + for alias := range aliases.GetAliasesForJsonApiTypeAndAlternates(aliasType.JsonApiType, aliasType.AlternateJsonApiTypesForAliases) { + results = append(results, alias) + } } } } else if attribute.Type == "SINGULAR_RESOURCE_TYPE" { @@ -327,10 +333,12 @@ func Complete(c Request) ([]string, cobra.ShellCompDirective) { if c.Type&CompleteAlias > 0 { jsonApiType := c.Resource.JsonApiType - aliasesForJsonApiType := aliases.GetAliasesForJsonApiTypeAndAlternates(jsonApiType, c.Resource.AlternateJsonApiTypesForAliases) + if !c.NoAliases { + aliasesForJsonApiType := aliases.GetAliasesForJsonApiTypeAndAlternates(jsonApiType, c.Resource.AlternateJsonApiTypesForAliases) - for alias := range aliasesForJsonApiType { - results = append(results, alias) + for alias := range aliasesForJsonApiType { + results = append(results, alias) + } } } diff --git a/external/json/to_json.go b/external/json/to_json.go index cae531e..72c471b 100644 --- a/external/json/to_json.go +++ b/external/json/to_json.go @@ -15,7 +15,7 @@ var segmentRegex = regexp.MustCompile("(.+?)(\\[[0-9]+])?$") var attributeWithArrayIndex = regexp.MustCompile("\\[[0-9]+]") -func ToJson(args []string, noWrapping bool, compliant bool, attributes map[string]*resources.CrudEntityAttribute) (string, error) { +func ToJson(args []string, noWrapping bool, compliant bool, attributes map[string]*resources.CrudEntityAttribute, useAliases bool) (string, error) { if len(args)%2 == 1 { 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)) @@ -37,13 +37,13 @@ func ToJson(args []string, noWrapping bool, compliant bool, attributes map[strin } if firstArrayKeyIdx >= 0 { - return toJsonArray(args, noWrapping, compliant, attributes) + return toJsonArray(args, noWrapping, compliant, attributes, useAliases) } else { - return toJsonObject(args, noWrapping, compliant, attributes) + return toJsonObject(args, noWrapping, compliant, attributes, useAliases) } } -func toJsonObject(args []string, noWrapping bool, compliant bool, attributes map[string]*resources.CrudEntityAttribute) (string, error) { +func toJsonObject(args []string, noWrapping bool, compliant bool, attributes map[string]*resources.CrudEntityAttribute, useAliases bool) (string, error) { var result interface{} = make(map[string]interface{}) @@ -96,7 +96,9 @@ func toJsonObject(args []string, noWrapping bool, compliant bool, attributes map } if aliasType, ok := resources.GetResourceByName(resourceType); ok { - val = aliases.ResolveAliasValuesOrReturnIdentity(aliasType.JsonApiType, aliasType.AlternateJsonApiTypesForAliases, val, aliasAttributeToUse) + if useAliases { + val = aliases.ResolveAliasValuesOrReturnIdentity(aliasType.JsonApiType, aliasType.AlternateJsonApiTypesForAliases, val, aliasAttributeToUse) + } } else { log.Warnf("Could not find a resource for %s, this is a bug.", resourceType) } @@ -106,7 +108,9 @@ func toJsonObject(args []string, noWrapping bool, compliant bool, attributes map if len(splitAlias) == 4 { if splitAlias[0] == "alias" { - val = aliases.ResolveAliasValuesOrReturnIdentity(splitAlias[1], []string{}, splitAlias[2], splitAlias[3]) + if useAliases { + val = aliases.ResolveAliasValuesOrReturnIdentity(splitAlias[1], []string{}, splitAlias[2], splitAlias[3]) + } } } } @@ -138,7 +142,7 @@ func toJsonObject(args []string, noWrapping bool, compliant bool, attributes map } -func toJsonArray(args []string, noWrapping bool, compliant bool, attributes map[string]*resources.CrudEntityAttribute) (string, error) { +func toJsonArray(args []string, noWrapping bool, compliant bool, attributes map[string]*resources.CrudEntityAttribute, useAliases bool) (string, error) { var result interface{} = make([]interface{}, 0) diff --git a/external/json/to_json_test.go b/external/json/to_json_test.go index 49a0da7..af1772a 100644 --- a/external/json/to_json_test.go +++ b/external/json/to_json_test.go @@ -18,7 +18,7 @@ func TestErrorMessageWhenOddNumberOfValuesPassed(t *testing.T) { 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?") // Execute SUT - _, actual := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}) + _, actual := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}, true) // Verification if actual.Error() != expected.Error() { @@ -32,7 +32,7 @@ func TestToJsonLegacyFormatEmptyValue(t *testing.T) { expected := `{"data":{}}` // Execute SUT - actual, _ := ToJson(input, false, false, map[string]*resources.CrudEntityAttribute{}) + actual, _ := ToJson(input, false, false, map[string]*resources.CrudEntityAttribute{}, true) // Verification if actual != expected { @@ -46,7 +46,7 @@ func TestToJsonLegacyFormatSimpleKeyStringValue(t *testing.T) { expected := `{"data":{"key":"val"}}` // Execute SUT - actual, _ := ToJson(input, false, false, map[string]*resources.CrudEntityAttribute{}) + actual, _ := ToJson(input, false, false, map[string]*resources.CrudEntityAttribute{}, true) // Verification if actual != expected { @@ -60,7 +60,7 @@ func TestToJsonLegacyFormatSimpleNestedKeyValue(t *testing.T) { expected := `{"data":{"foo":{"bar":"val"}}}` // Execute SUT - actual, _ := ToJson(input, false, false, map[string]*resources.CrudEntityAttribute{}) + actual, _ := ToJson(input, false, false, map[string]*resources.CrudEntityAttribute{}, true) // Verification if actual != expected { @@ -74,7 +74,7 @@ func TestToJsonLegacyFormatSimpleKeyNumericValue(t *testing.T) { expected := `{"data":{"key":3}}` // Execute SUT - actual, _ := ToJson(input, false, false, map[string]*resources.CrudEntityAttribute{}) + actual, _ := ToJson(input, false, false, map[string]*resources.CrudEntityAttribute{}, true) // Verification if actual != expected { @@ -88,7 +88,7 @@ func TestToJsonLegacyFormatSimpleKeyFloatNumericValue(t *testing.T) { expected := `{"data":{"key":3.3}}` // Execute SUT - actual, _ := ToJson(input, false, false, map[string]*resources.CrudEntityAttribute{}) + actual, _ := ToJson(input, false, false, map[string]*resources.CrudEntityAttribute{}, true) // Verification if actual != expected { @@ -102,7 +102,7 @@ func TestToJsonLegacyFormatSimpleKeyBooleanTrueValue(t *testing.T) { expected := `{"data":{"key":true}}` // Execute SUT - actual, _ := ToJson(input, false, false, map[string]*resources.CrudEntityAttribute{}) + actual, _ := ToJson(input, false, false, map[string]*resources.CrudEntityAttribute{}, true) // Verification if actual != expected { @@ -116,7 +116,7 @@ func TestToJsonLegacyFormatSimpleKeyBooleanFalseValue(t *testing.T) { expected := `{"data":{"key":false}}` // Execute SUT - actual, _ := ToJson(input, false, false, map[string]*resources.CrudEntityAttribute{}) + actual, _ := ToJson(input, false, false, map[string]*resources.CrudEntityAttribute{}, true) // Verification if actual != expected { @@ -130,7 +130,7 @@ func TestToJsonLegacyFormatSimpleKeyNullValue(t *testing.T) { expected := `{"data":{"key":null}}` // Execute SUT - actual, _ := ToJson(input, false, false, map[string]*resources.CrudEntityAttribute{}) + actual, _ := ToJson(input, false, false, map[string]*resources.CrudEntityAttribute{}, true) // Verification if actual != expected { @@ -144,7 +144,7 @@ func TestToJsonLegacyFormatSimpleKeyEmptyArrayValue(t *testing.T) { expected := `{"data":{"key":[]}}` // Execute SUT - actual, _ := ToJson(input, false, false, map[string]*resources.CrudEntityAttribute{}) + actual, _ := ToJson(input, false, false, map[string]*resources.CrudEntityAttribute{}, true) // Verification if actual != expected { @@ -159,7 +159,7 @@ func TestToJsonLegacyFormatSimpleArrayIndexValue(t *testing.T) { expected := `{"data":{"key":["val"]}}` // Execute SUT - actual, _ := ToJson(input, false, false, map[string]*resources.CrudEntityAttribute{}) + actual, _ := ToJson(input, false, false, map[string]*resources.CrudEntityAttribute{}, true) // Verification if actual != expected { @@ -173,7 +173,7 @@ func TestToJsonLegacyFormatSimpleArrayWithTwoValues(t *testing.T) { expected := `{"data":{"key":["val","val2"]}}` // Execute SUT - actual, _ := ToJson(input, false, false, map[string]*resources.CrudEntityAttribute{}) + actual, _ := ToJson(input, false, false, map[string]*resources.CrudEntityAttribute{}, true) // Verification if actual != expected { @@ -187,7 +187,7 @@ func TestToJsonCompliantFormatEmptyValue(t *testing.T) { expected := `{"data":{}}` // Execute SUT - actual, _ := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}) + actual, _ := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}, true) // Verification if actual != expected { @@ -201,7 +201,7 @@ func TestToJsonCompliantFormatSimpleKeyStringValue(t *testing.T) { expected := `{"data":{"attributes":{"key":"val"}}}` // Execute SUT - actual, _ := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}) + actual, _ := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}, true) // Verification if actual != expected { @@ -215,7 +215,7 @@ func TestToJsonCompliantFormatSimpleKeyStringValueAttributesKeyNotDoubleEncoded( expected := `{"data":{"attributes":{"key":"val"}}}` // Execute SUT - actual, _ := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}) + actual, _ := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}, true) // Verification if actual != expected { @@ -229,7 +229,7 @@ func TestToJsonCompliantFormatSimpleKeyStringValueRelationshipsKeyNotDoubleEncod expected := `{"data":{"relationships":{"key":"val"}}}` // Execute SUT - actual, _ := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}) + actual, _ := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}, true) // Verification if actual != expected { @@ -243,7 +243,7 @@ func TestToJsonCompliantFormatSimpleKeyWithTypeStringValue(t *testing.T) { expected := `{"data":{"type":"val"}}` // Execute SUT - actual, _ := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}) + actual, _ := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}, true) // Verification if actual != expected { @@ -257,7 +257,7 @@ func TestToJsonCompliantFormatSimpleKeyWithIdStringValue(t *testing.T) { expected := `{"data":{"id":"val"}}` // Execute SUT - actual, _ := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}) + actual, _ := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}, true) // Verification if actual != expected { @@ -271,7 +271,7 @@ func TestToJsonCompliantFormatSimpleNestedKeyValue(t *testing.T) { expected := `{"data":{"attributes":{"foo":{"bar":"val"}}}}` // Execute SUT - actual, _ := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}) + actual, _ := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}, true) // Verification if actual != expected { @@ -285,7 +285,7 @@ func TestToJsonCompliantFormatSimpleKeyNumericValue(t *testing.T) { expected := `{"data":{"attributes":{"key":3}}}` // Execute SUT - actual, _ := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}) + actual, _ := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}, true) // Verification if actual != expected { @@ -299,7 +299,7 @@ func TestToJsonCompliantFormatSimpleKeyBooleanTrueValue(t *testing.T) { expected := `{"data":{"attributes":{"key":true}}}` // Execute SUT - actual, _ := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}) + actual, _ := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}, true) // Verification if actual != expected { @@ -313,7 +313,7 @@ func TestToJsonCompliantFormatSimpleKeyBooleanFalseValue(t *testing.T) { expected := `{"data":{"attributes":{"key":false}}}` // Execute SUT - actual, _ := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}) + actual, _ := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}, true) // Verification if actual != expected { @@ -327,7 +327,7 @@ func TestToJsonCompliantFormatSimpleKeyNullValue(t *testing.T) { expected := `{"data":{"attributes":{"key":null}}}` // Execute SUT - actual, _ := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}) + actual, _ := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}, true) // Verification if actual != expected { @@ -341,7 +341,7 @@ func TestToJsonCompliantFormatSimpleKeyEmptyArrayValue(t *testing.T) { expected := `{"data":{"attributes":{"key":[]}}}` // Execute SUT - actual, _ := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}) + actual, _ := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}, true) // Verification if actual != expected { @@ -356,7 +356,7 @@ func TestToJsonCompliantFormatSimpleArrayIndexValue(t *testing.T) { expected := `{"data":{"attributes":{"key":["val"]}}}` // Execute SUT - actual, _ := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}) + actual, _ := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}, true) // Verification if actual != expected { @@ -370,7 +370,7 @@ func TestToJsonCompliantFormatSimpleArrayWithTwoValues(t *testing.T) { expected := `{"data":{"attributes":{"key":["val","val2"]}}}` // Execute SUT - actual, _ := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}) + actual, _ := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}, true) // Verification if actual != expected { @@ -401,7 +401,7 @@ func TestToJsonCompliantForAlias(t *testing.T) { require.True(t, ok, "Should have gotten a resource back") // Execute SUT - actual, _ := ToJson(input, false, false, res.Attributes) + actual, _ := ToJson(input, false, false, res.Attributes, true) // Verification if actual != expected { @@ -432,7 +432,7 @@ func TestToJsonCompliantForAliasWithWildcard(t *testing.T) { require.True(t, ok, "Should have gotten a resource back") // Execute SUT - actual, _ := ToJson(input, true, false, res.Attributes) + actual, _ := ToJson(input, true, false, res.Attributes, true) // Verification if actual != expected { @@ -456,7 +456,7 @@ func TestToJsonCompliantAliasForNestedArray(t *testing.T) { expected := `{"data":{"attributes":{"components":{"shampoo":{"options":[{"id":"123-456"}]}}}}}` // Execute SUT - actual, _ := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}) + actual, _ := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}, true) // Verification if actual != expected { @@ -470,7 +470,7 @@ func TestToJsonErrorsWhenArrayAndObjectKeysSpecified(t *testing.T) { expected := fmt.Errorf("detected both array syntax arguments '[0]' and object syntax arguments 'key'. Only one format can be used") // Execute SUT - _, actual := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}) + _, actual := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}, true) // Verification if actual.Error() != expected.Error() { @@ -484,7 +484,7 @@ func TestToJsonCreatesSimpleSingleElementArrayWhenArrayKeysSpecified(t *testing. expected := `{"data":["val"]}` // Execute SUT - actual, _ := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}) + actual, _ := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}, true) // Verification if actual != expected { @@ -498,7 +498,7 @@ func TestToJsonCreatesSimpleSingleElementArrayWithNoWrappingWhenArrayKeysSpecifi expected := `["val"]` // Execute SUT - actual, _ := ToJson(input, true, true, map[string]*resources.CrudEntityAttribute{}) + actual, _ := ToJson(input, true, true, map[string]*resources.CrudEntityAttribute{}, true) // Verification if actual != expected { @@ -512,7 +512,7 @@ func TestToJsonCreatesMultipleElementArrayWhenArrayKeysSpecified(t *testing.T) { expected := `{"data":["foo","bar"]}` // Execute SUT - actual, _ := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}) + actual, _ := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}, true) // Verification if actual != expected { @@ -526,7 +526,7 @@ func TestToJsonCreatesMultipleElementArrayWhenArrayKeysSpecifiedAndSomeMissing(t expected := `{"data":["foo",null,null,"bar"]}` // Execute SUT - actual, _ := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}) + actual, _ := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}, true) // Verification if actual != expected { @@ -540,7 +540,7 @@ func TestToJsonCreatesSimpleSingleElementArrayOfObjectWhenArrayKeysSpecified(t * expected := `{"data":[{"bar":"val"}]}` // Execute SUT - actual, _ := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}) + actual, _ := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}, true) // Verification if actual != expected { @@ -554,7 +554,7 @@ func TestToJsonCreatesComplexSingleElementArrayOfObjectWhenArrayKeysSpecified(t expected := `{"data":[{"bar":"val","foo":"zoo"},{"bar":"tree"}]}` // Execute SUT - actual, _ := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}) + actual, _ := ToJson(input, false, true, map[string]*resources.CrudEntityAttribute{}, true) // Verification if actual != expected { diff --git a/external/resources/uritemplates.go b/external/resources/uritemplates.go index f31edac..9a6b763 100644 --- a/external/resources/uritemplates.go +++ b/external/resources/uritemplates.go @@ -67,7 +67,7 @@ func GenerateUrlViaIdableAttributes(urlInfo *CrudEntityInfo, args []id.IdableAtt } -func GenerateUrl(urlInfo *CrudEntityInfo, args []string) (string, error) { +func GenerateUrl(urlInfo *CrudEntityInfo, args []string, useAliases bool) (string, error) { template, err := uritemplate.New(urlInfo.Url) if err != nil { @@ -91,7 +91,11 @@ func GenerateUrl(urlInfo *CrudEntityInfo, args []string) (string, error) { log.Tracef("url %s uses a type [%s] instead of id, so URL will be filled with this", urlInfo.Url, override) attribute = override } - values[varName] = uritemplate.String(aliases.ResolveAliasValuesOrReturnIdentity(varType.JsonApiType, varType.AlternateJsonApiTypesForAliases, args[idx], attribute)) + if useAliases { + values[varName] = uritemplate.String(aliases.ResolveAliasValuesOrReturnIdentity(varType.JsonApiType, varType.AlternateJsonApiTypesForAliases, args[idx], attribute)) + } else { + values[varName] = uritemplate.String(args[idx]) + } } else { log.Warnf("Could not find a resource with type %s, aliases are probably broken", resourceType) values[varName] = uritemplate.String(args[idx]) diff --git a/external/resources/uritemplates_test.go b/external/resources/uritemplates_test.go index ad88b48..8d88558 100644 --- a/external/resources/uritemplates_test.go +++ b/external/resources/uritemplates_test.go @@ -208,7 +208,7 @@ func TestGenerateUrlHappyPathWithSlugParentResourceValueOverride(t *testing.T) { // Execute SUT - actualUrl, err := GenerateUrl(&crudEntityInfo, []string{"slug=test"}) + actualUrl, err := GenerateUrl(&crudEntityInfo, []string{"slug=test"}, true) // Verification @@ -247,7 +247,46 @@ func TestGenerateUrlHappyPathWithNoParentResourceValueOverride(t *testing.T) { // Execute SUT - actualUrl, err := GenerateUrl(&crudEntityInfo, []string{"name=Ron_Swanson"}) + actualUrl, err := GenerateUrl(&crudEntityInfo, []string{"name=Ron_Swanson"}, true) + + // Verification + + if err != nil { + t.Errorf("Should not have gotten error when generating URL.") + } + + if actualUrl != expectedUrlWithId { + t.Errorf("Url should have been %s but got %s", expectedUrlWithId, actualUrl) + } +} + +func TestGenerateUrlHappyPathWithNoParentResourceValueOverrideAndNoAliasSubstitution(t *testing.T) { + // Fixture Setup + + err := aliases.ClearAllAliases() + if err != nil { + t.Errorf("Couldn't create test fixtures, error while cleaning aliases, %v", err) + } + + crudEntityInfo := getValidCrudEntityInfo() + crudEntityInfo.Url = "/v2/customers/{customers}" + crudEntityInfo.ParentResourceValueOverrides = map[string]string{} + + flowExample := `{ + "data": { + "id": "123", + "type": "customer", + "name": "Ron Swanson" + } +}` + + aliases.SaveAliasesForResources(flowExample) + + expectedUrlWithId := "/v2/customers/name=Ron_Swanson" + + // Execute SUT + + actualUrl, err := GenerateUrl(&crudEntityInfo, []string{"name=Ron_Swanson"}, false) // Verification