Skip to content

Commit

Permalink
Add support for attributes with multiple types (#51)
Browse files Browse the repository at this point in the history
* Add support for attributes with multiple types
  • Loading branch information
buzzy authored Jan 27, 2023
1 parent 40a8c72 commit d8238fc
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 9 deletions.
15 changes: 14 additions & 1 deletion autocomplete.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,24 @@ func collectFlagsAndOptions() map[string]map[string][]string {
if attribute.Value.Enum != nil {

for _, enum := range attribute.Value.Enum {
allFlags[command][flagName] = append(allFlags[command][flagName], enum.(string))
allFlags[command][flagName] = append(allFlags[command][flagName], enum.(string)+" ")
}

}

//Collect valid flag values in case of AnyOf
if attribute.Value.AnyOf != nil {
for _, item := range attribute.Value.AnyOf {

if item.Value.Enum != nil {
for _, enum := range item.Value.Enum {
allFlags[command][flagName] = append(allFlags[command][flagName], enum.(string)+" ")
}
}

}
}

}

}
Expand Down
16 changes: 15 additions & 1 deletion command.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,21 @@ func parseCommand(format string, verbose bool) {
continue
}

switch attribute.Value.Type {
theType := attribute.Value.Type

//If no type is specified, look deeper
if theType == "" && attribute.Value.AnyOf != nil {
for _, item := range attribute.Value.AnyOf {

if item.Value.Type != "" {
theType = item.Value.Type
break
}

}
}

switch theType {
case "relationship":
//Special case for arrays in relationships
for _, item := range strings.Split(value, ",") {
Expand Down
30 changes: 23 additions & 7 deletions help.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"encoding/json"
"flag"
"fmt"
"regexp"
Expand Down Expand Up @@ -57,13 +56,13 @@ func printHelp() {
for _, path := range doc.Paths {
for _, method := range path.Operations() {

var group string
group := ""

json.Unmarshal(method.ExtensionProps.Extensions["x-resource"].(json.RawMessage), &group)

//Fallback to Tag if x-resource group is missing
if group == "" {
if method.Extensions["x-resource"] == nil {
//Fallback to Tag if x-resource group is missing
group = strings.Title(method.Tags[0])
} else {
group = method.Extensions["x-resource"].(string)
}

//Add a space before each uppercase letter
Expand Down Expand Up @@ -252,11 +251,28 @@ func printHelpCommand(command string) {
theType = inheritType
}

enum := attribute.Value.Enum

//Special case: ProviderConfiguration and maybe others?
if theType == "" && attribute.Value.AnyOf != nil {
for _, item := range attribute.Value.AnyOf {

if item.Value.Enum != nil {
enum = item.Value.Enum
}

if item.Value.Type != "" {
theType = item.Value.Type
}

}
}

flags[flagName] = Parameter{
varType: theType,
description: description,
required: required,
enum: attribute.Value.Enum,
enum: enum,
}

}
Expand Down

0 comments on commit d8238fc

Please sign in to comment.