Skip to content

Commit

Permalink
Changed so that comments can be staked to structures and structure fi…
Browse files Browse the repository at this point in the history
…elds.
  • Loading branch information
nao1215 committed Dec 28, 2024
1 parent a171ab1 commit f7905af
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 52 deletions.
75 changes: 51 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,43 +29,47 @@ import (
//go:generate go run main.go

func main() {
// Step 1: Define the ValueObject configuration
vo := vogen.ValueObject{
StructName: "Person",
Fields: []vogen.Field{
{Name: "Name", Type: "string"},
{Name: "Age", Type: "int"},
},
Imports: []string{"fmt"},
}

// Step 2: Create a Vogen instance with custom file path and package name
// Step 1: Create a Vogen instance with custom file path and package name.
// By default, the file path is "value_objects.go" and the package name is "vo".
gen, err := vogen.New(
vogen.WithFilePath(filepath.Join("..", "example_vo.go")),
vogen.WithPackageName("vo"),
vogen.WithFilePath(filepath.Join("testdata", "example_output.go")),
vogen.WithPackageName("vo_example"),
)
if err != nil {
fmt.Printf("Failed to create Vogen instance: %v\n", err)
return
}

// Step 3: Append the ValueObject definition
if err := gen.AppendValueObjects(vo); err != nil {
// Step 2: Append the ValueObject definition
if err := gen.AppendValueObjects(
vogen.ValueObject{
StructName: "Person",
Fields: []vogen.Field{
{Name: "Name", Type: "string", Comments: []string{"Name is the name of the person."}},
{Name: "Age", Type: "int", Comments: []string{"Age is the age of the person."}},
},
Comments: []string{
"Person is a Value Object to describe the feature of vogen.",
"This is sample comment.",
},
},
// Use auto generated comments.
vogen.ValueObject{
StructName: "Address",
Fields: []vogen.Field{
{Name: "City", Type: "string"},
},
},
); err != nil {
fmt.Printf("Failed to append ValueObject: %v\n", err)
return
}

// Step 4: Generate the code
// Step 3: Generate the code
if err := gen.Generate(); err != nil {
fmt.Printf("Failed to generate code: %v\n", err)
return
}

// Step 5: Output success message
fmt.Println("Code generated successfully. Check '../example_vo.go' for the output.")

// Output:
// Code generated successfully. Check '../example_vo.go' for the output.
}
```

Expand All @@ -79,10 +83,13 @@ import (
"fmt"
)

// Person represents a value object.
// Person is a Value Object to describe the feature of vogen.
// This is sample comment.
type Person struct {
// Name is the name of the person.
name string
age int
// Age is the age of the person.
age int
}

// NewPerson creates a new instance of Person.
Expand All @@ -104,6 +111,26 @@ func (o Person) Age() int {
func (o Person) Equal(other Person) bool {
return o.Name() == other.Name() && o.Age() == other.Age()
}

// Address represents a value object.
type Address struct {
city string
}

// NewAddress creates a new instance of Address.
func NewAddress(city string) Address {
return Address{city: city}
}

// City returns the city field.
func (o Address) City() string {
return o.city
}

// Equal checks if two Address objects are equal.
func (o Address) Equal(other Address) bool {
return o.City() == other.City()
}
```

## License
Expand Down
39 changes: 23 additions & 16 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,7 @@ import (
)

func ExampleVogen_Generate() {
// Step 1: Define the ValueObject configuration
vo := vogen.ValueObject{
StructName: "Person",
Fields: []vogen.Field{
{Name: "Name", Type: "string"},
{Name: "Age", Type: "int"},
},
Imports: []string{"fmt"},
}

// Step 2: Create a Vogen instance with custom file path and package name
// Step 1: Create a Vogen instance with custom file path and package name
gen, err := vogen.New(
vogen.WithFilePath(filepath.Join("testdata", "example_output.go")),
vogen.WithPackageName("vo_example"),
Expand All @@ -28,21 +18,38 @@ func ExampleVogen_Generate() {
return
}

// Step 3: Append the ValueObject definition
if err := gen.AppendValueObjects(vo); err != nil {
// Step 2: Append the ValueObject definition
if err := gen.AppendValueObjects(
vogen.ValueObject{
StructName: "Person",
Fields: []vogen.Field{
{Name: "Name", Type: "string", Comments: []string{"Name is the name of the person."}},
{Name: "Age", Type: "int", Comments: []string{"Age is the age of the person."}},
},
Comments: []string{
"Person is a Value Object to describe the feature of vogen.",
"This is sample comment.",
},
},
// Use auto generated comments.
vogen.ValueObject{
StructName: "Address",
Fields: []vogen.Field{
{Name: "City", Type: "string"},
},
},
); err != nil {
fmt.Printf("Failed to append ValueObject: %v\n", err)
return
}

// Step 4: Generate the code
// Step 3: Generate the code
if err := gen.Generate(); err != nil {
fmt.Printf("Failed to generate code: %v\n", err)
return
}

// Step 5: Output success message
fmt.Println("Code generated successfully. Check 'example_output.go' for the output.")

// Output:
// Code generated successfully. Check 'example_output.go' for the output.
}
43 changes: 31 additions & 12 deletions vogen.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,34 @@ import (
// ValueObject is set to the metadata of the ValueObject to be automatically generated.
type ValueObject struct {
// StructName is the name of the struct to be generated.
// required.
// Required field.
StructName string
// Fields is the list of fields to be generated.
// required.
// Required field.
Fields []Field
// Comments is the struct comment to be generated.
// No need to add '//' to indicate the start of a comment.
// Optional Field. If not specified, the struct comment is automatically generated.
Comments []string
// Imports is the list of imports to be generated.
// optional.
// Optional Field.
Imports []string
}

// Field is set to the metadata of the field to be automatically generated.
type Field struct {
// Name is the name of the field to be generated.
// The name specified in Name is always converted to Lowercase.
// required.
// Required field.
Name string
// Type is the type of the field to be generated.
// Type can be a primitive type or a Defined type. If you specify a Defined type, you must specify package import.
// required.
// Required field.
Type string
// Comments is the field comment to be generated.
// No need to add '//' to indicate the start of a comment.
// Optional Field. If not specified, the field comment is to be empty.
Comments []string
}

// lowercaseName returns the field name in lowercase.
Expand Down Expand Up @@ -120,25 +128,36 @@ func (vo *Vogen) Generate() error {
// writeImports writes the import statements to the code.
func (vo *Vogen) writeImports() {
importSet := map[string]struct{}{}

importSet["fmt"] = struct{}{}
for _, valueObject := range vo.valueObjects {
for _, imp := range valueObject.Imports {
importSet[imp] = struct{}{}
}
}
if len(importSet) > 0 {
vo.code = append(vo.code, "import (\n")
for imp := range importSet {
vo.code = append(vo.code, fmt.Sprintf("\t\"%s\"\n", imp))
}
vo.code = append(vo.code, ")\n\n")

vo.code = append(vo.code, "import (\n")
for imp := range importSet {
vo.code = append(vo.code, fmt.Sprintf("\t\"%s\"\n", imp))
}
vo.code = append(vo.code, ")\n\n")
}

// writeStruct writes the struct to the code.
func (vo *Vogen) writeStruct(valueObject ValueObject) {
vo.code = append(vo.code, fmt.Sprintf("// %s represents a value object.\n", valueObject.StructName))
if len(valueObject.Comments) > 0 {
for _, comment := range valueObject.Comments {
vo.code = append(vo.code, fmt.Sprintf("// %s\n", comment))
}
} else {
vo.code = append(vo.code, fmt.Sprintf("// %s represents a value object.\n", valueObject.StructName))
}

vo.code = append(vo.code, fmt.Sprintf("type %s struct {\n", valueObject.StructName))
for _, field := range valueObject.Fields {
for _, comment := range field.Comments {
vo.code = append(vo.code, fmt.Sprintf("\t// %s\n", comment))
}
vo.code = append(vo.code, fmt.Sprintf("\t%s %s\n", field.lowercaseName(), field.Type))
}
vo.code = append(vo.code, "}\n\n")
Expand Down

0 comments on commit f7905af

Please sign in to comment.