-
Notifications
You must be signed in to change notification settings - Fork 50
Implement auto-generated spec.Resolve(targetKey) method with maintainable code generation #727
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
Draft
Copilot
wants to merge
10
commits into
main
Choose a base branch
from
copilot/fix-486
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1817389
Initial plan
Copilot bcadff3
Add ResolvedSpec struct and core functionality
Copilot 0bc9a23
Add ResolvedSpec support to frontend functions
Copilot 2633da3
Add resolved spec examples and helper functions
Copilot fd3faf4
Fix syntax error in frontend/request.go
Copilot bdd0c96
Complete ResolvedSpec refactor with documentation
Copilot 2e1f706
Implement simple spec.Resolve(targetKey) approach
Copilot 189ba71
Create gen-resolve command to auto-generate Resolve method
Copilot 6f2e709
Add documentation for code generation system
Copilot e9b42a8
Fix generator to dynamically detect target-specific fields and add co…
Copilot 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,272 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "fmt" | ||
| "go/ast" | ||
| "go/format" | ||
| "go/parser" | ||
| "go/token" | ||
| "os" | ||
| "sort" | ||
| ) | ||
|
|
||
| func main() { | ||
| if len(os.Args) != 2 { | ||
| fmt.Fprintf(os.Stderr, "Usage: %s <output-file>\n", os.Args[0]) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| outputFile := os.Args[1] | ||
|
|
||
| // Parse the source files to extract struct information | ||
| specFields, targetFields, err := extractStructFields() | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "Error extracting struct fields: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| // Generate the code | ||
| code, err := generateResolveMethod(specFields, targetFields) | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "Error generating code: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| // Write to output file | ||
| err = os.WriteFile(outputFile, code, 0644) | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "Error writing output file: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| fmt.Printf("Generated Resolve method in %s\n", outputFile) | ||
| } | ||
|
|
||
| type FieldInfo struct { | ||
| Name string // Field name (e.g. "Name") | ||
| TypeName string // Type name (e.g. "string") | ||
| IsSlice bool // Is it a slice type | ||
| IsMap bool // Is it a map type | ||
| IsPtr bool // Is it a pointer type | ||
| } | ||
|
|
||
| func extractStructFields() ([]FieldInfo, []FieldInfo, error) { | ||
| // Parse spec.go for Spec struct | ||
| specFields, err := parseStructFromFile("spec.go", "Spec") | ||
| if err != nil { | ||
| return nil, nil, fmt.Errorf("failed to extract Spec fields: %w", err) | ||
| } | ||
|
|
||
| // Parse target.go for Target struct | ||
| targetFields, err := parseStructFromFile("target.go", "Target") | ||
| if err != nil { | ||
| return nil, nil, fmt.Errorf("failed to extract Target fields: %w", err) | ||
| } | ||
|
|
||
| return specFields, targetFields, nil | ||
| } | ||
|
|
||
| func parseStructFromFile(filename, structName string) ([]FieldInfo, error) { | ||
| fset := token.NewFileSet() | ||
| node, err := parser.ParseFile(fset, filename, nil, parser.ParseComments) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to parse %s: %w", filename, err) | ||
| } | ||
|
|
||
| var fields []FieldInfo | ||
|
|
||
| // Find the target struct | ||
| ast.Inspect(node, func(n ast.Node) bool { | ||
| if ts, ok := n.(*ast.TypeSpec); ok && ts.Name.Name == structName { | ||
| if st, ok := ts.Type.(*ast.StructType); ok { | ||
| for _, field := range st.Fields.List { | ||
| if len(field.Names) == 0 { | ||
| continue // Skip embedded fields | ||
| } | ||
|
|
||
| fieldName := field.Names[0].Name | ||
|
|
||
| // Skip unexported fields and internal fields | ||
| if !ast.IsExported(fieldName) || fieldName == "extensions" || fieldName == "decodeOpts" { | ||
| continue | ||
| } | ||
|
|
||
| typeName, isSlice, isMap, isPtr := parseFieldType(field.Type) | ||
|
|
||
| fields = append(fields, FieldInfo{ | ||
| Name: fieldName, | ||
| TypeName: typeName, | ||
| IsSlice: isSlice, | ||
| IsMap: isMap, | ||
| IsPtr: isPtr, | ||
| }) | ||
| } | ||
| } | ||
| } | ||
| return true | ||
| }) | ||
|
|
||
| // Sort fields for consistent output | ||
| sort.Slice(fields, func(i, j int) bool { | ||
| return fields[i].Name < fields[j].Name | ||
| }) | ||
|
|
||
| return fields, nil | ||
| } | ||
|
|
||
| func parseFieldType(expr ast.Expr) (typeName string, isSlice, isMap, isPtr bool) { | ||
| switch t := expr.(type) { | ||
| case *ast.Ident: | ||
| return t.Name, false, false, false | ||
| case *ast.StarExpr: | ||
| name, slice, mp, _ := parseFieldType(t.X) | ||
| return name, slice, mp, true | ||
| case *ast.ArrayType: | ||
| if t.Len == nil { // slice | ||
| name, _, mp, ptr := parseFieldType(t.Elt) | ||
| return name, true, mp, ptr | ||
| } | ||
| return "array", false, false, false // fixed-size array | ||
| case *ast.MapType: | ||
| return "map", false, true, false | ||
| case *ast.SelectorExpr: | ||
| // Handle qualified identifiers like pkg.Type | ||
| if ident, ok := t.X.(*ast.Ident); ok { | ||
| return fmt.Sprintf("%s.%s", ident.Name, t.Sel.Name), false, false, false | ||
| } | ||
| return "selector", false, false, false | ||
| default: | ||
| return "unknown", false, false, false | ||
| } | ||
| } | ||
|
|
||
| func generateResolveMethod(specFields []FieldInfo, targetFields []FieldInfo) ([]byte, error) { | ||
| var buf bytes.Buffer | ||
|
|
||
| // Generate file header | ||
| buf.WriteString(`// Code generated by cmd/gen-resolve. DO NOT EDIT. | ||
|
|
||
| package dalec | ||
|
|
||
| `) | ||
|
|
||
| // Determine which fields exist in both Spec and Target structs | ||
| targetFieldNames := make(map[string]bool) | ||
| for _, field := range targetFields { | ||
| targetFieldNames[field.Name] = true | ||
| } | ||
|
|
||
| // Fields that need special merge logic (exist in both structs) | ||
| var targetOverrideFields []FieldInfo | ||
| var regularFields []FieldInfo | ||
|
|
||
| for _, field := range specFields { | ||
| // Skip Targets field itself | ||
| if field.Name == "Targets" { | ||
| continue | ||
| } | ||
|
|
||
| if targetFieldNames[field.Name] { | ||
| targetOverrideFields = append(targetOverrideFields, field) | ||
| } else { | ||
| regularFields = append(regularFields, field) | ||
| } | ||
| } | ||
|
|
||
| // Start the Resolve method | ||
| buf.WriteString("// Resolve creates a new Spec with target-specific configuration merged in.\n") | ||
| buf.WriteString("// This eliminates the need to pass targetKey parameters around by pre-resolving\n") | ||
| buf.WriteString("// all target-specific configuration into a single Spec.\n") | ||
| buf.WriteString("func (s *Spec) Resolve(targetKey string) *Spec {\n") | ||
| buf.WriteString("\t// Create a deep copy of the current spec\n") | ||
| buf.WriteString("\tresolved := &Spec{\n") | ||
|
|
||
| // Copy regular fields (no target overrides) | ||
| for _, field := range regularFields { | ||
| buf.WriteString(fmt.Sprintf("\t\t%s: s.%s,\n", field.Name, field.Name)) | ||
| } | ||
|
|
||
| buf.WriteString("\t}\n\n") | ||
|
|
||
| // Copy extension fields | ||
| buf.WriteString("\t// Copy extension fields\n") | ||
| buf.WriteString("\tif s.extensions != nil {\n") | ||
| buf.WriteString("\t\tresolved.extensions = make(extensionFields)\n") | ||
| buf.WriteString("\t\tfor k, v := range s.extensions {\n") | ||
| buf.WriteString("\t\t\tresolved.extensions[k] = v\n") | ||
| buf.WriteString("\t\t}\n") | ||
| buf.WriteString("\t}\n\n") | ||
|
|
||
| // Handle target override fields dynamically | ||
| buf.WriteString("\t// Get target-specific configuration\n") | ||
| buf.WriteString("\ttarget, hasTarget := s.Targets[targetKey]\n\n") | ||
|
|
||
| for _, field := range targetOverrideFields { | ||
| generateFieldMergeLogic(&buf, field) | ||
| buf.WriteString("\n") | ||
| } | ||
|
|
||
| buf.WriteString("\t// Clear targets as this is now a resolved spec for a specific target\n") | ||
| buf.WriteString("\tresolved.Targets = nil\n\n") | ||
|
|
||
| buf.WriteString("\treturn resolved\n") | ||
| buf.WriteString("}\n") | ||
|
|
||
| // Format the generated code | ||
| formatted, err := format.Source(buf.Bytes()) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to format generated code: %w", err) | ||
| } | ||
|
|
||
| return formatted, nil | ||
| } | ||
|
|
||
| func generateFieldMergeLogic(buf *bytes.Buffer, field FieldInfo) { | ||
| switch field.Name { | ||
| case "Tests": | ||
| // Tests are appended (global + target-specific) | ||
| buf.WriteString(fmt.Sprintf("\t// Merge %s (global + target-specific)\n", field.Name)) | ||
| buf.WriteString(fmt.Sprintf("\tresolved.%s = append([]*TestSpec(nil), s.%s...)\n", field.Name, field.Name)) | ||
| buf.WriteString("\tif hasTarget && target.Tests != nil {\n") | ||
| buf.WriteString(fmt.Sprintf("\t\tresolved.%s = append(resolved.%s, target.%s...)\n", field.Name, field.Name, field.Name)) | ||
| buf.WriteString("\t}") | ||
|
|
||
| case "Dependencies": | ||
| // Dependencies use special GetPackageDeps logic | ||
| buf.WriteString(fmt.Sprintf("\t// Resolve %s using existing merge logic\n", field.Name)) | ||
| buf.WriteString(fmt.Sprintf("\tresolved.%s = s.GetPackageDeps(targetKey)", field.Name)) | ||
|
|
||
| case "Image": | ||
| // Image uses special MergeSpecImage logic | ||
| buf.WriteString(fmt.Sprintf("\t// Resolve %s using existing merge logic\n", field.Name)) | ||
| buf.WriteString(fmt.Sprintf("\tresolved.%s = MergeSpecImage(s, targetKey)", field.Name)) | ||
|
|
||
| case "Artifacts": | ||
| // Artifacts use GetArtifacts logic | ||
| buf.WriteString(fmt.Sprintf("\t// Resolve %s using existing logic\n", field.Name)) | ||
| buf.WriteString(fmt.Sprintf("\tresolved.%s = s.GetArtifacts(targetKey)", field.Name)) | ||
|
|
||
| case "Provides", "Replaces", "Conflicts": | ||
| // These use the existing Get* methods | ||
| methodName := "Get" + field.Name | ||
| buf.WriteString(fmt.Sprintf("\t// Resolve %s using existing logic\n", field.Name)) | ||
| buf.WriteString(fmt.Sprintf("\tresolved.%s = s.%s(targetKey)", field.Name, methodName)) | ||
|
|
||
| case "PackageConfig": | ||
| // PackageConfig: target overrides global | ||
| buf.WriteString(fmt.Sprintf("\t// Resolve %s (target overrides global)\n", field.Name)) | ||
| buf.WriteString(fmt.Sprintf("\tresolved.%s = s.%s\n", field.Name, field.Name)) | ||
| buf.WriteString(fmt.Sprintf("\tif hasTarget && target.%s != nil {\n", field.Name)) | ||
| buf.WriteString(fmt.Sprintf("\t\tresolved.%s = target.%s\n", field.Name, field.Name)) | ||
| buf.WriteString("\t}") | ||
|
|
||
| default: | ||
| // Default: target overrides global (for future fields) | ||
| buf.WriteString(fmt.Sprintf("\t// Resolve %s (target overrides global)\n", field.Name)) | ||
| buf.WriteString(fmt.Sprintf("\tresolved.%s = s.%s\n", field.Name, field.Name)) | ||
| buf.WriteString(fmt.Sprintf("\tif hasTarget && target.%s != nil {\n", field.Name)) | ||
| buf.WriteString(fmt.Sprintf("\t\tresolved.%s = target.%s\n", field.Name, field.Name)) | ||
| buf.WriteString("\t}") | ||
| } | ||
| } | ||
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.
@copilot This generator needs tests.
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.
Added comprehensive test suite for the generator covering field type parsing, struct field extraction, and code generation logic. The tests ensure the generator works correctly with the actual Spec and Target structs. See e9b42a8.