-
Notifications
You must be signed in to change notification settings - Fork 20
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(wip): schema struct gen #80
base: master
Are you sure you want to change the base?
Conversation
WalkthroughThis pull request introduces a new command-line application entry point in the main package that processes arguments to execute specific commands. When the "ssg" command is provided, control is passed to a schema generator function in the ssg package. The generator analyzes Go source code using AST parsing to collect struct metadata and generate corresponding Go files from a template. Additionally, new test types and go:generate directives in a test package validate the schema generation functionality. Changes
Sequence Diagram(s)sequenceDiagram
participant U as User
participant M as Main (cmd/zog/main.go)
participant S as SchemaGen (cmd/zog/ssg/generator.go)
U->>M: Execute application with "ssg" command
M->>S: Call Run(osArgs)
S->>S: Retrieve GOFILE & GOLINE environment variables
S->>S: Parse Go file and aggregate struct metadata via AST
S->>S: Generate output file using predefined template
S-->>M: Return generation result or error
M-->>U: Display output or error message
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 1
🧹 Nitpick comments (4)
cmd/zog/ssg/generator.go (3)
35-39
: Handle direct invocation gracefully.
Currently, the logic depends on the GOFILE environment variable, which is set automatically by go:generate. If users call this tool directly (not via go:generate), GOFILE won't be set and will trigger a fatal error. Consider adding a usage message or fallback behavior to make direct invocation more user-friendly.
62-83
: Avoid potential fragility in matching the struct declaration line.
Relying on fset.Position(x.Pos()).Line == genLine+1 may skip the target struct if extra blank or comment lines appear. Consider a more robust approach, such as matching the type name directly or scanning for the first encountered type after the directive.
197-216
: Implement narrower schema type mappings.
The placeholder code defaults all field types to zog.ZogSchema. Mapping known primitives (e.g., string, int) to specialized schema types will increase clarity and correctness, especially once the package moves beyond an initial proof-of-concept.ssgtest/some_test.go (1)
1-1
: Consider renaming the package for clarity.
Using package main in a test file may be confusing if you plan to run tests with “go test”. To keep consistency, consider placing these structs in a ssgtest or similar package name.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
cmd/zog/main.go
(1 hunks)cmd/zog/ssg/generator.go
(1 hunks)ssgtest/some_test.go
(1 hunks)
switch os.Args[1] { | ||
case "ssg": | ||
ssg.Run(os.Args[2:]) | ||
default: | ||
fmt.Println("Unknown command") | ||
} |
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.
Prevent potential index out-of-range panic.
Calling os.Args[1] directly can crash if no arguments are passed. Ensure that len(os.Args) > 1 before switching on os.Args[1].
Apply this fix:
func main() {
+ if len(os.Args) < 2 {
+ fmt.Println("Usage: zog [ssg]")
+ os.Exit(1)
+ }
switch os.Args[1] {
case "ssg":
ssg.Run(os.Args[2:])
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
switch os.Args[1] { | |
case "ssg": | |
ssg.Run(os.Args[2:]) | |
default: | |
fmt.Println("Unknown command") | |
} | |
func main() { | |
if len(os.Args) < 2 { | |
fmt.Println("Usage: zog [ssg]") | |
os.Exit(1) | |
} | |
switch os.Args[1] { | |
case "ssg": | |
ssg.Run(os.Args[2:]) | |
default: | |
fmt.Println("Unknown command") | |
} | |
} |
Hey! Thanks for making this first draft! I see what you meant when you said we were talking past each other. Now I think I get what you were going for, you are supposed to pass the generated struct's to the z.Struct() constructor. type userSchemaType {
Name ZogSchema
...
}
type User struct {
Name string
....
}
userSchema := z.Struct(userSchemaType{
// this is type safe
Name: z.String()...
}) Right? |
you are welcome! ... yes! that is exactly what i want to archive. how do you like the idea + technical approach here? |
Sorry for taking so long reply. I have been thinking about it. Honestly, I was not sure I liked it for a long while. And I still want to think about it a little more. But there is a massive benefit to this approach I had not thought of: It allows us to have typesafe Omit, Pick and Extend methods. Which is VERY nice. So let me think about it a bit more and get back to you |
@Oudwins thanks for your response! |
ok, basic idea is working:
/ssgtest
pkg you will find a file with some struct definitions + the go gen cmdgo generate ./ssgtest/some_test.go
in the project rootZogSchema
🎉open 2dos:
StringSchema
type=> how do you like it?
Summary by CodeRabbit
Summary by CodeRabbit
New Features
Tests