-
Notifications
You must be signed in to change notification settings - Fork 544
feat: Add codegen #5618
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
Merged
kodiakhq
merged 6 commits into
cloudquery:main
from
erezrokah:feat/azure_devops_codegen
Dec 15, 2022
Merged
feat: Add codegen #5618
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
401593d
feat: Add codegen
erezrokah 292b6d5
refactor: Use Resources var and init function like Azure plugin
erezrokah 80729dc
refactor: Extract write to function
erezrokah a481949
refactor: Use pointer for code gen
erezrokah 259ee31
Merge branch 'main' into feat/azure_devops_codegen
erezrokah a6231fa
Update go.mod
erezrokah 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 hidden or 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 hidden or 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,18 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "log" | ||
|
|
||
| "github.com/cloudquery/cloudquery/plugins/source/azuredevops/codegen/recipes" | ||
| ) | ||
|
|
||
| func main() { | ||
| for _, resource := range recipes.Resources { | ||
| if err := resource.Generate(); err != nil { | ||
| log.Fatal(err) | ||
| } | ||
| } | ||
| if err := recipes.GenerateTablesList(recipes.Resources); err != nil { | ||
| log.Fatal(err) | ||
| } | ||
| } | ||
This file contains hidden or 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,120 @@ | ||
| package recipes | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "embed" | ||
| "fmt" | ||
| "go/format" | ||
| "os" | ||
| "path" | ||
| "reflect" | ||
| "runtime" | ||
| "text/template" | ||
|
|
||
| "github.com/cloudquery/plugin-sdk/codegen" | ||
| "github.com/cloudquery/plugin-sdk/schema" | ||
| "github.com/google/uuid" | ||
| "github.com/iancoleman/strcase" | ||
| ) | ||
|
|
||
| //go:embed templates/*.go.tpl | ||
| var templatesFS embed.FS | ||
| var Resources []*Resource | ||
|
|
||
| type Resource struct { | ||
| Service string | ||
|
candiduslynx marked this conversation as resolved.
|
||
| SubService string | ||
| Struct interface{} | ||
| Table *codegen.TableDefinition | ||
| } | ||
|
|
||
| func writeTemplateContentToFile(dir string, filePath string, buff bytes.Buffer) error { | ||
| outputPath := path.Join(dir, "../..", filePath) | ||
| outputDir := path.Dir(outputPath) | ||
| if err := os.MkdirAll(outputDir, os.ModePerm); err != nil { | ||
| return fmt.Errorf("failed to create directory %s: %w", outputDir, err) | ||
| } | ||
|
|
||
| content := buff.Bytes() | ||
| formattedContent, err := format.Source(content) | ||
| if err != nil { | ||
| fmt.Printf("failed to format source: %s: %v\n", filePath, err) | ||
| } else { | ||
| content = formattedContent | ||
| } | ||
|
|
||
| if err := os.WriteFile(outputPath, content, 0644); err != nil { | ||
| return fmt.Errorf("failed to write file %s: %w", filePath, err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func renderTemplate(name string, filePath string, data interface{}) error { | ||
| _, filename, _, ok := runtime.Caller(0) | ||
| if !ok { | ||
| return fmt.Errorf("failed to get caller information") | ||
| } | ||
| dir := path.Dir(filename) | ||
|
|
||
| tpl, err := template.New(name).Funcs(template.FuncMap{"ToCamel": strcase.ToCamel}).ParseFS(templatesFS, "templates/*.go.tpl") | ||
|
candiduslynx marked this conversation as resolved.
|
||
| if err != nil { | ||
| return fmt.Errorf("failed to parse azureDevops templates: %w", err) | ||
| } | ||
| tpl, err = tpl.ParseFS(codegen.TemplatesFS, "templates/*.go.tpl") | ||
| if err != nil { | ||
| return fmt.Errorf("failed to parse sdk template: %w", err) | ||
| } | ||
|
|
||
| var buff bytes.Buffer | ||
| if err := tpl.Execute(&buff, data); err != nil { | ||
| return fmt.Errorf("failed to execute template: %w", err) | ||
| } | ||
|
|
||
| return writeTemplateContentToFile(dir, filePath, buff) | ||
| } | ||
|
|
||
| func (resource *Resource) generate() error { | ||
| return renderTemplate("resource.go.tpl", path.Join("resources", "services", resource.Service, resource.SubService+".go"), resource) | ||
| } | ||
|
|
||
| func isUUID(fieldType reflect.Type) bool { | ||
| fieldKind := fieldType.Kind() | ||
|
|
||
| if fieldKind == reflect.Ptr { | ||
| return isUUID(fieldType.Elem()) | ||
| } | ||
|
|
||
| return fieldType == reflect.TypeOf(uuid.UUID{}) | ||
| } | ||
|
|
||
| func typeTransformer(field reflect.StructField) (schema.ValueType, error) { | ||
| if isUUID(field.Type) { | ||
| return schema.TypeUUID, nil | ||
| } | ||
|
|
||
| return codegen.DefaultTypeTransformer(field) | ||
|
candiduslynx marked this conversation as resolved.
|
||
| } | ||
|
|
||
| func (resource *Resource) Generate() error { | ||
| var err error | ||
| resource.Table, err = codegen.NewTableFromStruct( | ||
| fmt.Sprintf("azuredevops_%s_%s", resource.Service, resource.SubService), | ||
|
erezrokah marked this conversation as resolved.
|
||
| resource.Struct, | ||
| codegen.WithTypeTransformer(typeTransformer), | ||
|
candiduslynx marked this conversation as resolved.
|
||
| ) | ||
|
|
||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| resource.Table.Resolver = "fetch" + strcase.ToCamel(resource.SubService) | ||
| if err := resource.generate(); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func GenerateTablesList(resources []*Resource) error { | ||
| return renderTemplate("tables.go.tpl", path.Join("resources", "plugin", "tables.go"), resources) | ||
| } | ||
This file contains hidden or 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,20 @@ | ||
| package recipes | ||
|
|
||
| import ( | ||
| "github.com/microsoft/azure-devops-go-api/azuredevops/v6/core" | ||
| ) | ||
|
|
||
| func init() { | ||
| resources := []*Resource{ | ||
| { | ||
| SubService: "projects", | ||
| Struct: &core.TeamProjectReference{}, | ||
| }, | ||
| } | ||
|
|
||
| for _, resource := range resources { | ||
| resource.Service = "core" | ||
| } | ||
|
|
||
| Resources = append(Resources, resources...) | ||
| } |
11 changes: 11 additions & 0 deletions
11
plugins/source/azuredevops/codegen/recipes/templates/resource.go.tpl
This file contains hidden or 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,11 @@ | ||
| // Code generated by codegen; DO NOT EDIT. | ||
|
|
||
| package {{.Service}} | ||
|
|
||
| import ( | ||
| "github.com/cloudquery/plugin-sdk/schema" | ||
| ) | ||
|
|
||
| func {{.SubService | ToCamel}}() *schema.Table { | ||
|
candiduslynx marked this conversation as resolved.
|
||
| return &schema.Table{{template "table.go.tpl" .Table}} | ||
| } | ||
18 changes: 18 additions & 0 deletions
18
plugins/source/azuredevops/codegen/recipes/templates/tables.go.tpl
This file contains hidden or 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,18 @@ | ||
| // Code generated by codegen; DO NOT EDIT. | ||
|
|
||
| package plugin | ||
|
|
||
| import ( | ||
| {{- range .}} | ||
| "github.com/cloudquery/cloudquery/plugins/source/azuredevops/resources/services/{{.Service}}" | ||
| {{- end}} | ||
| "github.com/cloudquery/plugin-sdk/schema" | ||
| ) | ||
|
|
||
| func tables() []*schema.Table { | ||
| return []*schema.Table{ | ||
| {{- range .}} | ||
| {{.Service}}.{{.SubService | ToCamel}}(), | ||
| {{- end}} | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
77 changes: 47 additions & 30 deletions
77
plugins/source/azuredevops/resources/services/core/projects.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.