-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: hide resources #254
Merged
Merged
feat: hide resources #254
Changes from 3 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,27 +71,11 @@ func GetTemplateData(fileName string) (TemplateData, error) { | |
return TemplateData{}, err | ||
} | ||
|
||
resources := make(map[string]ResourceData) | ||
for _, r := range spec.Tags { | ||
if strings.Contains(r.Name, "(beta)") { | ||
// skip beta resources for now | ||
continue | ||
} | ||
|
||
resourceName, resourceKey := getResourceNames(r.Name) | ||
resources[resourceKey] = ResourceData{ | ||
GoName: strcase.ToCamel(resourceName), | ||
DisplayName: strings.ToLower(resourceName), | ||
Description: jsonString(r.Description), | ||
Operations: make(map[string]OperationData, 0), | ||
} | ||
} | ||
|
||
resources := NewResources(spec.Tags) | ||
for path, pathItem := range spec.Paths.Map() { | ||
for method, op := range pathItem.Operations() { | ||
tag := op.Tags[0] // each op only has one tag | ||
if strings.Contains(tag, "(beta)") { | ||
// skip beta resources for now | ||
if shouldFilter(tag) { | ||
continue | ||
} | ||
_, resourceKey := getResourceNames(tag) | ||
|
@@ -149,6 +133,42 @@ func GetTemplateData(fileName string) (TemplateData, error) { | |
return TemplateData{Resources: resources}, nil | ||
} | ||
|
||
func NewResourceData(tag openapi3.Tag) ResourceData { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Made some constructor functions to encapsulate the rules for creating a resource data struct type. This is simpler to test than setting up JSON and calling |
||
resourceName, _ := getResourceNames(tag.Name) | ||
|
||
return ResourceData{ | ||
GoName: strcase.ToCamel(resourceName), | ||
DisplayName: strings.ToLower(resourceName), | ||
Description: jsonString(tag.Description), | ||
Operations: make(map[string]OperationData, 0), | ||
} | ||
} | ||
|
||
func NewResources(tags openapi3.Tags) map[string]ResourceData { | ||
resources := make(map[string]ResourceData) | ||
for _, t := range tags { | ||
if shouldFilter(t.Name) { | ||
continue | ||
} | ||
|
||
resourceName, resourceKey := getResourceNames(t.Name) | ||
resources[resourceKey] = ResourceData{ | ||
GoName: strcase.ToCamel(resourceName), | ||
DisplayName: strings.ToLower(resourceName), | ||
Description: jsonString(t.Description), | ||
Operations: make(map[string]OperationData, 0), | ||
} | ||
} | ||
|
||
return resources | ||
} | ||
|
||
func shouldFilter(name string) bool { | ||
return strings.Contains(name, "(beta)") || | ||
strings.ToLower(name) == "other" || | ||
strings.ToLower(name) == "oauth2 clients" | ||
} | ||
|
||
func NewResourceCmd(parentCmd *cobra.Command, analyticsTracker analytics.Tracker, resourceName, shortDescription, longDescription string) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: resourceName, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package resources_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/getkin/kin-openapi/openapi3" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"ldcli/cmd/resources" | ||
) | ||
|
||
func TestNewResourceData(t *testing.T) { | ||
t.Run("converts OpenAPI spec to a resource", func(t *testing.T) { | ||
tag := openapi3.Tag{ | ||
Name: "My resources", | ||
Description: "description", | ||
} | ||
|
||
resource := resources.NewResourceData(tag) | ||
|
||
assert.Equal(t, "MyResources", resource.GoName) | ||
assert.Equal(t, "my resources", resource.DisplayName) | ||
assert.Equal(t, `"description"`, resource.Description) | ||
}) | ||
} | ||
|
||
func TestNewResources(t *testing.T) { | ||
t.Run("builds a map of resources", func(t *testing.T) { | ||
tags := openapi3.Tags{ | ||
{ | ||
Name: "My resources", | ||
}, | ||
{ | ||
Name: "My resources2", | ||
}, | ||
} | ||
|
||
rs := resources.NewResources(tags) | ||
|
||
require.Len(t, rs, 2) | ||
assert.Equal(t, "MyResources", rs["my-resources"].GoName) | ||
assert.Equal(t, "MyResources2", rs["my-resources-2"].GoName) | ||
}) | ||
|
||
t.Run("skips certain endpoints", func(t *testing.T) { | ||
tests := map[string]struct { | ||
name string | ||
}{ | ||
"skips beta endpoints": { | ||
name: "My resources (beta)", | ||
}, | ||
"skips oauth2- endpoints": { | ||
name: "OAuth2 clients", | ||
}, | ||
"skips other endpoints": { | ||
name: "Other", | ||
}, | ||
} | ||
for name, tt := range tests { | ||
tt := tt | ||
t.Run(name, func(t *testing.T) { | ||
tags := openapi3.Tags{ | ||
{ | ||
Name: tt.name, | ||
}, | ||
} | ||
|
||
rs := resources.NewResources(tags) | ||
|
||
assert.Len(t, rs, 0) | ||
}) | ||
} | ||
}) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can run
make generate
if that's easier.