Skip to content

Commit

Permalink
Resolves #342 - Improve performance of help screens (#359)
Browse files Browse the repository at this point in the history
  • Loading branch information
steve-r-west authored Jun 19, 2023
1 parent 4d95095 commit 8e190ef
Show file tree
Hide file tree
Showing 14 changed files with 141 additions and 84 deletions.
4 changes: 2 additions & 2 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 12 additions & 9 deletions cmd/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand All @@ -648,6 +648,7 @@ func GetCreateExample(resource resources.Resource) string {
Verb: completion.Create,
Attribute: k,
ToComplete: "",
NoAliases: true,
})

arg := `"Hello World"`
Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -725,6 +726,7 @@ func GetUpdateExample(resource resources.Resource) string {
Verb: completion.Update,
Attribute: k,
ToComplete: "",
NoAliases: true,
})

arg := `"Hello World"`
Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -793,6 +795,7 @@ func GetDeleteExample(resource resources.Resource) string {
Verb: completion.Delete,
Attribute: k,
ToComplete: "",
NoAliases: true,
})

arg := `"Hello World"`
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmd/reset-store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion cmd/test-json.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
2 changes: 0 additions & 2 deletions external/aliases/aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down
40 changes: 24 additions & 16 deletions external/completion/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type Request struct {
Attribute string
QueryParam string
ToComplete string
NoAliases bool
}

func Complete(c Request) ([]string, cobra.ShellCompDirective) {
Expand Down Expand Up @@ -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" {
Expand Down Expand Up @@ -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)
}
}
}

Expand Down
18 changes: 11 additions & 7 deletions external/json/to_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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{})

Expand Down Expand Up @@ -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)
}
Expand All @@ -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])
}
}
}
}
Expand Down Expand Up @@ -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)

Expand Down
Loading

0 comments on commit 8e190ef

Please sign in to comment.