你了解領域驅動設計(DDD)嗎?我還是沒能完全理解。
最近,我一直在深入研究有關領域驅動設計(DDD)的書籍。在DDD中,出現了值物件的概念。值物件主要具有以下特徵(與本文主題無關的元素被故意省略):
為了滿足 Golang 中的上述規範,實作需要如下所示:
type Person struct { name string } func NewPerson(name string) Person { return Person{name: name} } func (o Person) Name() string { return o.name } func (o Person) Equal(other Person) bool { return o.Name() == other.Name() }
坦白說,實現這種功能很麻煩。此外,為 Getter() 或 Equal() 編寫單元測試感覺毫無意義。我經常發現自己希望「如果 Golang 有像 Kotlin 的值類別或資料類別那樣的東西就好了。」
nao1215/vogen 套件是使用 New()、Getter 和 Equal() 方法自動產生值物件程式碼的函式庫。此名稱代表“值物件產生器”。
使用這個函式庫,您可以在 Golang 中編寫值物件的元數據,並根據該元資料自動產生程式碼。這個規範的靈感來自 shogo82148/myddlmaker(一個從元資料產生 DB DDL 的函式庫)。
典型用法包括在 value_object/gen/main.go 中定義元資料並執行 gogenerate ./... 產生 value_object/value_object.go 檔案。也可以將輸出分佈到多個文件中。
以下是 value_object/gen/main.go 的範例實作。
package main import ( "fmt" "path/filepath" "github.com/nao1215/vogen" ) //go:generate go run main.go func main() { // 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("testdata", "example_output.go")), vogen.WithPackageName("vo_example"), ) if err != nil { fmt.Printf("Failed to create Vogen instance: %v\n", err) return } // 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 3: Generate the code if err := gen.Generate(); err != nil { fmt.Printf("Failed to generate code: %v\n", err) return } }
在 vogen.New() 中,您可以指定產生程式碼的檔案路徑和套件名稱,但這些是可選的。如果省略,vo包下預設會產生一個value_objects.go檔。
vogen.ValueObject() 對應元資料。結構及其字段的註釋是可選的。如果省略,輸出將包括毫無靈魂的英文註釋。對於類型,您可以指定定義類型(使用者定義類型),但在這種情況下,您還必須提供模組路徑。由於定義類型尚未經過測試,我特意從範例程式碼中省略了它們(計劃稍後進行測試)。
以下是使用上面的範例自動產生的程式碼:
// Code generated by vogen. DO NOT EDIT. package vo_example import ( "fmt" ) // 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 is the age of the person. age int } // NewPerson creates a new instance of Person. func NewPerson(name string, age int) Person { return Person{name: name, age: age} } // Name returns the name field. func (o Person) Name() string { return o.name } // Age returns the age field. func (o Person) Age() int { return o.age } // Equal checks if two Person objects are equal. 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() }
我想知道 Golang 用戶是否會對像 vogen 套件這樣的功能感興趣。
以上是vogen -golang 中的值物件產生器的詳細內容。更多資訊請關注PHP中文網其他相關文章!