Hone logo
Hone
Problems

Simple Code Generator in Go

This challenge asks you to build a basic code generator in Go. Code generation is a powerful technique used to automate the creation of repetitive code, reducing development time and potential errors. Your generator will take a simple data structure definition as input and produce Go code representing that structure.

Problem Description

You are tasked with creating a Go program that generates Go code for structs based on a provided input. The input will be a slice of strings, where each string represents a field in the struct, formatted as "fieldName:fieldType". The program should output a complete Go struct definition, including the type and struct keywords, field names, and field types.

What needs to be achieved:

  • Parse a slice of strings representing struct fields.
  • Generate a valid Go struct definition string.
  • Handle potential errors gracefully (e.g., invalid input format).

Key Requirements:

  • The generated code must be syntactically correct Go code.
  • The generated code should include the type and struct keywords.
  • Each field in the input slice should be represented as a field in the generated struct.
  • The output should be a single string containing the complete struct definition.

Expected Behavior:

The program should take a slice of strings as input and return a string containing the generated Go struct definition. If the input is invalid (e.g., a field doesn't contain a colon), the program should return an error message.

Edge Cases to Consider:

  • Empty input slice: Should generate an empty struct definition (e.g., type MyStruct struct {}).
  • Fields with spaces in their names: Should handle these correctly.
  • Invalid field format (missing colon): Should return an error.
  • Multiple fields of the same name (should generate code, but consider if this is desired behavior).

Examples

Example 1:

Input: ["Name:string", "Age:int", "City:string"]
Output:
```go
type MyStruct struct {
    Name string
    Age int
    City string
}

Explanation: The input slice contains three fields. The program generates a Go struct named MyStruct with three fields: Name of type string, Age of type int, and City of type string.

Example 2:

Input: []
Output:
```go
type MyStruct struct {}

Explanation: The input slice is empty. The program generates an empty struct named MyStruct.

Example 3:

Input: ["Name:string", "Age"]
Output: Error: Invalid field format: Age

Explanation: The second field "Age" is missing the colon separator. The program returns an error message indicating the invalid format.

Constraints

  • The input slice will contain strings.
  • Field types will be limited to string, int, bool, float64. (You don't need to validate this, just use it as is).
  • The generated struct name will be MyStruct.
  • The program should handle invalid input gracefully and return an error message.
  • The generated code should be well-formatted (though strict formatting is not required).

Notes

  • Consider using string manipulation techniques to parse the field names and types.
  • Error handling is important. Provide informative error messages.
  • You can use fmt.Println to print the generated code or return it as a string.
  • Think about how to structure your code for readability and maintainability. Functions are your friend!
  • This is a simplified code generator. Real-world code generators are often much more complex. Focus on the core functionality of generating a basic struct definition.
Loading editor...
go