Go Code Generator for Simple Structs
This challenge involves building a Go program that can generate Go source code for simple struct definitions. This is a fundamental task in metaprogramming and can significantly streamline development by automating the creation of boilerplate code for data structures.
Problem Description
Your task is to create a Go program that takes a descriptive input and generates a corresponding Go struct definition as a string. The generated code should be valid Go syntax and represent a struct with fields of basic Go types.
Requirements:
- Input Format: The input will be a structured representation (e.g., JSON or a custom format you define) describing the struct's name and its fields. Each field will have a name and a type.
- Supported Types: The generator should support basic Go primitive types:
string,int,float64,bool. - Output Format: The output must be a valid Go source code string representing the struct. This string should include:
- A package declaration (e.g.,
package main). - The
typekeyword. - The struct name.
- The
structkeyword. - Field declarations, including the field name and its Go type.
- A package declaration (e.g.,
- Field Naming: Field names should start with an uppercase letter to be exported (public).
- No Methods or Tags: The generated struct should only contain fields. No methods, embedded types, or struct tags are required.
Expected Behavior:
Given an input that describes a struct, the program should produce a Go code string that, when compiled, defines that struct.
Edge Cases:
- Empty Struct: What happens if the input describes a struct with no fields?
- Invalid Input: While not strictly part of the core generation, consider how your input parsing might handle malformed descriptions (though for the challenge, you can assume valid input structure).
Examples
Example 1:
Input:
{
"structName": "User",
"fields": [
{"name": "Name", "type": "string"},
{"name": "Age", "type": "int"},
{"name": "IsActive", "type": "bool"}
]
}
package main
type User struct {
Name string
Age int
IsActive bool
}
Explanation: The input describes a User struct with three fields: Name (string), Age (int), and IsActive (bool). The output is the corresponding Go code.
Example 2:
Input:
{
"structName": "Product",
"fields": [
{"name": "ID", "type": "int"},
{"name": "Price", "type": "float64"},
{"name": "Description", "type": "string"}
]
}
package main
type Product struct {
ID int
Price float64
Description string
}
Explanation: Similar to Example 1, this generates a Product struct with different fields and types.
Example 3:
Input:
{
"structName": "EmptyConfig",
"fields": []
}
package main
type EmptyConfig struct {
}
Explanation: Handles the case of a struct with no fields.
Constraints
- The input will always be a valid JSON object describing the struct.
- Struct names and field names will be valid Go identifiers.
- Field types will only be one of
string,int,float64,bool. - The generated Go code should be syntactically correct and compilable.
- Performance is not a critical factor for this challenge, but the solution should be reasonably efficient.
Notes
- You will need to decide on the input data structure. A JSON object is suggested, but you can implement your own simple parser for a custom format if you prefer.
- Consider using Go's built-in string manipulation functions or a templating engine (like
text/template) for generating the Go code. - The primary goal is to understand how to programmatically create Go source code.