-
Notifications
You must be signed in to change notification settings - Fork 178
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
chore: PoC - Define make command to call code generation logic #2706
Changes from 4 commits
56261da
5fb9add
db24b5d
13fb973
c978425
3efaf97
0fd7f7f
57d74f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package codespec | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
type SnakeCaseString string | ||
|
||
func (snake SnakeCaseString) SnakeCase() string { | ||
return string(snake) | ||
} | ||
|
||
func (snake SnakeCaseString) PascalCase() string { | ||
words := strings.Split(string(snake), "_") | ||
var pascalCase string | ||
for i := range words { | ||
if words[i] != "" { | ||
pascalCase += strings.ToUpper(string(words[i][0])) + strings.ToLower(words[i][1:]) | ||
} | ||
} | ||
return pascalCase | ||
} | ||
|
||
func (snake SnakeCaseString) LowerCaseNoUnderscore() string { | ||
return strings.ReplaceAll(string(snake), "_", "") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,40 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"github.com/mongodb/terraform-provider-mongodbatlas/tools/codegen/codespec" | ||
"github.com/mongodb/terraform-provider-mongodbatlas/tools/codegen/openapi" | ||
"github.com/mongodb/terraform-provider-mongodbatlas/tools/codegen/schema" | ||
) | ||
|
||
const ( | ||
atlasAdminAPISpecURL = "https://raw.githubusercontent.com/mongodb/atlas-sdk-go/main/openapi/atlas-api-transformed.yaml" | ||
configPath = "schema-generation/config.yml" | ||
specFilePath = "open-api-spec.yml" | ||
configPath = "tools/codegen/config.yml" | ||
specFilePath = "tools/codegen/open-api-spec.yml" | ||
) | ||
|
||
func main() { | ||
resourceName := getOsArg() | ||
if resourceName == nil { | ||
log.Fatal("No resource name provided") | ||
} | ||
log.Printf("Resource name: %s\n", *resourceName) | ||
|
||
if err := openapi.DownloadOpenAPISpec(atlasAdminAPISpecURL, specFilePath); err != nil { | ||
log.Fatalf("an error occurred when downloading Atlas Admin API spec: %v", err) | ||
} | ||
|
||
_, err := codespec.ToCodeSpecModel(specFilePath, configPath, *resourceName) | ||
model, err := codespec.ToCodeSpecModel(specFilePath, configPath, resourceName) | ||
if err != nil { | ||
log.Fatalf("an error occurred while generating codespec.Model: %v", err) | ||
} | ||
|
||
for i := range model.Resources { | ||
resourceModel := model.Resources[i] | ||
schemaCode := schema.GenerateGoCode(resourceModel) | ||
if err := writeToFile(fmt.Sprintf("internal/service/%s/resource_schema.go", resourceModel.Name.LowerCaseNoUnderscore()), schemaCode); err != nil { | ||
log.Fatalf("an error occurred when writing content to file: %v", err) | ||
} | ||
} | ||
} | ||
|
||
func getOsArg() *string { | ||
|
@@ -37,3 +43,19 @@ func getOsArg() *string { | |
} | ||
return &os.Args[1] | ||
} | ||
|
||
func writeToFile(fileName, content string) error { | ||
// Open the file with write-only permission, create it if it doesn't exist and truncate its content if it exists | ||
file, err := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644) | ||
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
|
||
_, err = file.WriteString(content) | ||
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. knit: similar to this: https://github.com/mongodb/terraform-provider-mongodbatlas/blob/CLOUDP-278652_use_schemas/tools/check-changelog-entry-file/main.go#L35 there is os.WriteFile that might be a bit easier to use 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. thanks, indeed does simplify the code, adjusted |
||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package test_name | ||
package testname | ||
|
||
import ( | ||
"context" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package test_name | ||
package testname | ||
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. it's idiomatic Go to call the package same as last folder, here it is testdata but testname 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. This actually a golden file to verify the output of generated code. In the test the resource name is defined as |
||
|
||
import ( | ||
"context" | ||
|
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.
knit: this is used by our mock library, but I guess you already have everything you need: https://pkg.go.dev/github.com/huandu/xstrings#ToSnakeCase
via https://vektra.github.io/mockery/latest/configuration/#functions
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.
nice, adjusted to use an xstring implementation directly