Skip to content
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: generate teams operation data from openapi spec #226

Merged
merged 8 commits into from
May 1, 2024

Conversation

k3llymariee
Copy link
Contributor

@k3llymariee k3llymariee commented May 1, 2024

Adds a function that reads and parses an OpenAPI spec into our desired resources/operations data structs - for now it works with just the teams data (added that json temporarily at ld-teams-openapi.json to run locally).

Note: this new method isn't actually being called anywhere yet, but you can test this locally with the file above ☝️

Comment on lines 37 to 60
type OperationData struct {
Short string
Long string
Use string
Params []Param
HTTPMethod string
RequiresBody bool
Path string
}

type Param struct {
Name string
In string
Description string
Type string
Required bool
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved to cmd/resources/gen_resources.go since that's where these are added

err = json.Unmarshal(expectedFromFile, &expected)
require.NoError(t, err)

t.Run("succeeds with single get resource", func(t *testing.T) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not running the comparison directly on each op led to flakey tests - if there's a better way to do this LMK!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not getting test failures when running go test ./cmd/resources/ -run TestGetTemplateData -count=100 after changing the test to

t.Run("succeeds with single get resource", func(t *testing.T) {
	assert.Equal(t, expected, actual)
})

What failures were you seeing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...I swear I had some. But you're right, updated.

Required bool
}

func GetTemplateData(fileName string) (TemplateData, error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't actually being called anywhere but the tests yet, but I was hacking into main.go and passing in the file name to test it out locally. will work on actually calling this in a generate file in a follow up PR.

type ResourceData struct {
Name string
Description string
Operations map[string]*OperationData
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's probably not strictly necessary to have these as a map[string]*OperationData vs []*OperationData but it was helpful for pulling out a specific op type for testing.

@k3llymariee k3llymariee marked this pull request as ready for review May 1, 2024 00:38
@k3llymariee k3llymariee requested review from dbolson and sunnyguduru May 1, 2024 00:38
)

type TemplateData struct {
Resources map[string]*ResourceData
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we make these values instead of pointers? If we need to check for nil later we could either use

resources, ok := resources[tag]

or something like

// IsZero is true if this is a zero value struct type.
func (r ResourceData) IsZero() bool {
	return r.Name == "" && r.Description == "" && r.Operations == nil
}

if resource.IsZero() {
    // ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup!

err = json.Unmarshal(expectedFromFile, &expected)
require.NoError(t, err)

t.Run("succeeds with single get resource", func(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not getting test failures when running go test ./cmd/resources/ -run TestGetTemplateData -count=100 after changing the test to

t.Run("succeeds with single get resource", func(t *testing.T) {
	assert.Equal(t, expected, actual)
})

What failures were you seeing?

// probably won't need to keep this logging in but leaving it for debugging purposes
log.Printf("No response type defined for %s", op.OperationID)
} else {
for propName, _ := range schema.Value.Properties {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get a lint warning that this could be simplified to for propName := range.

@k3llymariee k3llymariee merged commit e96fb54 into main May 1, 2024
2 checks passed
@k3llymariee k3llymariee deleted the kelly/sc-241157/generate-teams-from-openapi-spec branch May 1, 2024 16:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants