forked from hypermodeinc/modusGraph
-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add modusgraphgen code generator #10
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
mlwelles
wants to merge
10
commits into
matthewmcneely:main
Choose a base branch
from
mlwelles:feature/add-modusgraphgen
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
8bf82db
feat: add modusgraphgen code generator
mlwelles 1a8553e
docs: add design for merging query into generated CLI
mlwelles 942f92f
docs: add implementation plan for merging query into generated CLI
mlwelles 98c2afb
refactor: rename cmd/modusgraphgen to cmd/modusgraph-gen
mlwelles 0404fa3
chore: add deprecation notice to standalone cmd/query
mlwelles 7df5b9e
feat: add QueryRaw method to generated client template
mlwelles 6660a26
feat: add query subcommand and --dir/--addr flags to generated CLI
mlwelles 3ab0726
test: add query subcommand tests and update golden files
mlwelles ec7efd6
chore: remove stray query binary and update gitignore
mlwelles d6f4145
chore: remove untracked files swept up by git add -A
mlwelles 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
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,277 @@ | ||
| // Package generator executes code-generation templates against a parsed model | ||
| // to produce typed client libraries. It embeds all templates from the templates/ | ||
| // directory and writes generated Go source files to the specified output directory. | ||
| package generator | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "embed" | ||
| "fmt" | ||
| "go/format" | ||
| "os" | ||
| "path/filepath" | ||
| "sort" | ||
| "strings" | ||
| "text/template" | ||
| "unicode" | ||
|
|
||
| "github.com/matthewmcneely/modusgraph/cmd/modusgraph-gen/internal/model" | ||
| ) | ||
|
|
||
| //go:embed templates/*.tmpl | ||
| var templateFS embed.FS | ||
|
|
||
| // header is prepended to every generated file. | ||
| const header = "// Code generated by modusGraphGen. DO NOT EDIT.\n\n" | ||
|
|
||
| // generateConfig holds optional configuration for Generate. | ||
| type generateConfig struct { | ||
| CLIDir string // Absolute path for CLI output; empty means default ({outputDir}/cmd/{name}). | ||
| } | ||
|
|
||
| // GenerateOption configures code generation. | ||
| type GenerateOption func(*generateConfig) | ||
|
|
||
| // WithCLIDir sets the output directory for the generated CLI main.go. | ||
| // When empty (the default), the CLI is generated at {outputDir}/cmd/{packageName}/main.go. | ||
| func WithCLIDir(dir string) GenerateOption { | ||
| return func(c *generateConfig) { c.CLIDir = dir } | ||
| } | ||
|
|
||
| // Generate renders all code-generation templates against pkg and writes the | ||
| // resulting Go source files into outputDir. The directory must already exist. | ||
| func Generate(pkg *model.Package, outputDir string, opts ...GenerateOption) error { | ||
| cfg := &generateConfig{} | ||
| for _, opt := range opts { | ||
| opt(cfg) | ||
| } | ||
|
|
||
| // Default CLIName to the package name if not explicitly set. | ||
| if pkg.CLIName == "" { | ||
| pkg.CLIName = pkg.Name | ||
| } | ||
| // Sort entities by name for deterministic output. | ||
| sort.Slice(pkg.Entities, func(i, j int) bool { | ||
| return pkg.Entities[i].Name < pkg.Entities[j].Name | ||
| }) | ||
| funcMap := template.FuncMap{ | ||
| "toLower": strings.ToLower, | ||
| "toUpper": strings.ToUpper, | ||
| "toSnakeCase": toSnakeCase, | ||
| "toCamelCase": toCamelCase, | ||
| "toLowerCamel": toLowerCamel, | ||
| "title": strings.Title, //nolint:staticcheck | ||
| "hasPrefix": strings.HasPrefix, | ||
| "hasSuffix": strings.HasSuffix, | ||
| "contains": strings.Contains, | ||
| "trimPrefix": strings.TrimPrefix, | ||
| "join": strings.Join, | ||
| "sub": func(a, b int) int { return a - b }, | ||
| "add": func(a, b int) int { return a + b }, | ||
|
|
||
| // Field helpers for templates. | ||
| "scalarFields": scalarFields, | ||
| "edgeFields": edgeFields, | ||
| "searchPredicate": searchPredicate, | ||
| "externalImports": externalImports, | ||
| } | ||
|
|
||
| tmpl, err := template.New("").Funcs(funcMap).ParseFS(templateFS, "templates/*.tmpl") | ||
| if err != nil { | ||
| return fmt.Errorf("parsing templates: %w", err) | ||
| } | ||
|
|
||
| // 1. client.go.tmpl → client_gen.go (once) | ||
| if err := executeAndWrite(tmpl, "client.go.tmpl", pkg, filepath.Join(outputDir, "client_gen.go")); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // 2. page_options.go.tmpl → page_options_gen.go (once) | ||
| if err := executeAndWrite(tmpl, "page_options.go.tmpl", pkg, filepath.Join(outputDir, "page_options_gen.go")); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // 3. iter.go.tmpl → iter_gen.go (once) | ||
| if err := executeAndWrite(tmpl, "iter.go.tmpl", pkg, filepath.Join(outputDir, "iter_gen.go")); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Per-entity templates. | ||
| type entityData struct { | ||
| PackageName string | ||
| Entity model.Entity | ||
| Entities []model.Entity | ||
| Imports map[string]string // Package alias → import path | ||
| } | ||
|
|
||
| for _, entity := range pkg.Entities { | ||
| data := entityData{ | ||
| PackageName: pkg.Name, | ||
| Entity: entity, | ||
| Entities: pkg.Entities, | ||
| Imports: pkg.Imports, | ||
| } | ||
| snake := toSnakeCase(entity.Name) | ||
|
|
||
| // 4. entity.go.tmpl → <snake>_gen.go | ||
| if err := executeAndWrite(tmpl, "entity.go.tmpl", data, filepath.Join(outputDir, snake+"_gen.go")); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // 5. options.go.tmpl → <snake>_options_gen.go | ||
| if err := executeAndWrite(tmpl, "options.go.tmpl", data, filepath.Join(outputDir, snake+"_options_gen.go")); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // 6. query.go.tmpl → <snake>_query_gen.go | ||
| if err := executeAndWrite(tmpl, "query.go.tmpl", data, filepath.Join(outputDir, snake+"_query_gen.go")); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| // 7. cli.go.tmpl → cmd/<name>/main.go (stub) | ||
| cliDir := cfg.CLIDir | ||
| if cliDir == "" { | ||
| cliDir = filepath.Join(outputDir, "cmd", pkg.Name) | ||
| } | ||
| if err := os.MkdirAll(cliDir, 0o755); err != nil { | ||
| return fmt.Errorf("creating CLI directory: %w", err) | ||
| } | ||
| if err := executeAndWrite(tmpl, "cli.go.tmpl", pkg, filepath.Join(cliDir, "main.go")); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // executeAndWrite renders a named template and writes the gofmt'd result to path. | ||
| func executeAndWrite(tmpl *template.Template, name string, data any, path string) error { | ||
| var buf bytes.Buffer | ||
| buf.WriteString(header) | ||
|
|
||
| if err := tmpl.ExecuteTemplate(&buf, name, data); err != nil { | ||
| return fmt.Errorf("executing template %s: %w", name, err) | ||
| } | ||
|
|
||
| // Format the output with gofmt. | ||
| formatted, err := format.Source(buf.Bytes()) | ||
| if err != nil { | ||
| // Write the unformatted output for debugging. | ||
| _ = os.WriteFile(path+".broken", buf.Bytes(), 0o644) | ||
| return fmt.Errorf("formatting %s: %w\nRaw output written to %s.broken", name, err, path) | ||
| } | ||
|
|
||
| if err := os.WriteFile(path, formatted, 0o644); err != nil { | ||
| return fmt.Errorf("writing %s: %w", path, err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // toSnakeCase converts a Go identifier like "ContentRating" to "content_rating". | ||
| func toSnakeCase(s string) string { | ||
| var result strings.Builder | ||
| for i, r := range s { | ||
| if unicode.IsUpper(r) { | ||
| if i > 0 { | ||
| prev := rune(s[i-1]) | ||
| if unicode.IsLower(prev) || (i+1 < len(s) && unicode.IsLower(rune(s[i+1]))) { | ||
| result.WriteRune('_') | ||
| } | ||
| } | ||
| result.WriteRune(unicode.ToLower(r)) | ||
| } else { | ||
| result.WriteRune(r) | ||
| } | ||
| } | ||
| return result.String() | ||
| } | ||
|
|
||
| // toCamelCase converts a snake_case or lowercase string to CamelCase. | ||
| func toCamelCase(s string) string { | ||
| parts := strings.Split(s, "_") | ||
| var result strings.Builder | ||
| for _, p := range parts { | ||
| if len(p) > 0 { | ||
| result.WriteString(strings.ToUpper(p[:1])) | ||
| result.WriteString(p[1:]) | ||
| } | ||
| } | ||
| return result.String() | ||
| } | ||
|
|
||
| // toLowerCamel converts an identifier to lowerCamelCase. | ||
| func toLowerCamel(s string) string { | ||
| if len(s) == 0 { | ||
| return s | ||
| } | ||
| // If already CamelCase, just lower the first letter. | ||
| return strings.ToLower(s[:1]) + s[1:] | ||
| } | ||
|
|
||
| // scalarFields returns fields that are not UID, DType, or edges. | ||
| func scalarFields(fields []model.Field) []model.Field { | ||
| var result []model.Field | ||
| for _, f := range fields { | ||
| if f.IsUID || f.IsDType || f.IsEdge { | ||
| continue | ||
| } | ||
| result = append(result, f) | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| // edgeFields returns only edge fields. | ||
| func edgeFields(fields []model.Field) []model.Field { | ||
| var result []model.Field | ||
| for _, f := range fields { | ||
| if f.IsEdge { | ||
| result = append(result, f) | ||
| } | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| // externalImports returns a sorted list of import paths needed by the given | ||
| // fields. It scans field GoTypes for package-qualified types (containing a dot | ||
| // that isn't "time."), looks up the full import path in the imports map, and | ||
| // returns the unique set. Standard library packages like "time" are excluded | ||
| // because templates handle them separately. | ||
| func externalImports(fields []model.Field, imports map[string]string) []string { | ||
| seen := make(map[string]bool) | ||
| var result []string | ||
| for _, f := range fields { | ||
| dot := strings.IndexByte(f.GoType, '.') | ||
| if dot < 0 { | ||
| continue | ||
| } | ||
| // Extract the package alias (e.g., "enums" from "enums.ResourceType"). | ||
| pkgAlias := f.GoType[:dot] | ||
| // Skip standard library packages that are handled directly in templates. | ||
| if pkgAlias == "time" { | ||
| continue | ||
| } | ||
| path, ok := imports[pkgAlias] | ||
| if !ok || seen[path] { | ||
| continue | ||
| } | ||
| seen[path] = true | ||
| result = append(result, path) | ||
| } | ||
| sort.Strings(result) | ||
| return result | ||
| } | ||
|
|
||
| // searchPredicate returns the dgraph predicate name for the entity's search | ||
| // field, or empty string if not searchable. | ||
| func searchPredicate(entity model.Entity) string { | ||
| if !entity.Searchable { | ||
| return "" | ||
| } | ||
| for _, f := range entity.Fields { | ||
| if f.Name == entity.SearchField { | ||
| return f.Predicate | ||
| } | ||
| } | ||
| return "" | ||
| } |
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.
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.
P2: Unscoped
.gitignorepatternquerywill ignore untracked files under existingcmd/query, making future additions easy to miss. Scope the ignore to the repo root if that's the intent.Prompt for AI agents