https://github.com/yukinagae/paramify
A build-function generator utilizing the Functional Options Pattern
https://github.com/yukinagae/paramify
functional-options functional-options-pattern generator go golang
Last synced: 5 months ago
JSON representation
A build-function generator utilizing the Functional Options Pattern
- Host: GitHub
- URL: https://github.com/yukinagae/paramify
- Owner: yukinagae
- License: apache-2.0
- Created: 2024-10-03T05:49:18.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-10-08T02:10:46.000Z (over 1 year ago)
- Last Synced: 2025-04-19T12:34:53.785Z (about 1 year ago)
- Topics: functional-options, functional-options-pattern, generator, go, golang
- Language: Go
- Homepage: https://github.com/uber-go/guide/blob/master/style.md#functional-options
- Size: 457 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# paramify
[](https://pkg.go.dev/github.com/yukinagae/paramify)
[](https://goreportcard.com/report/github.com/yukinagae/paramify)
[](https://github.com/yukinagae/paramify/blob/main/LICENSE)
[](https://github.com/yukinagae/paramify/releases)
[](https://github.com/yukinagae/paramify/pulse)
[](https://github.com/yukinagae/paramify/issues)
[](https://github.com/yukinagae/paramify/pulls)
[](https://github.com/yukinagae/paramify/activity)
`paramify` is a build-function generator tool based on the [Functional Options Pattern](https://github.com/uber-go/guide/blob/master/style.md#functional-options), allowing you to build structs with intuitive, flexible, and type-safe APIs.
## Installation
```bash
go install github.com/yukinagae/paramify/cmd/paramify@latest
```
## Usage
Create a struct with the fields you need. Use struct tags to mark optional fields with `omitempty`.
```go
//go:generate paramify -type=User
type User struct {
ID string `json:"id"`
Name string `json:"name"`
Age uint `json:"age,omitempty"`
Address *Address `json:"address,omitempty"`
Friends []string `json:"friends,omitempty"`
}
```
Run the following command to generate the necessary functions for building instances of your struct using the Functional Options Pattern.
```bash
$ go generate
```
Use the generated functions to create instances of your struct. Required fields are passed as arguments to the constructor function, while optional fields are set using functional options.
```go
func main() {
john := NewUser(
"1", // Required: ID
"John", // Required: Name
)
sam := NewUser(
"2", // Required: ID
"Sam", // Required: Name
WithUserAge(20), // Optional: Age
WithUserAddress(Address{"street"}), // Optional: Address
WithUserFriends([]string{"Jane"}), // Optional: Friends
)
}
```
### Example
Here's a complete example demonstrating the usage:
```go
package main
import "fmt"
//go:generate paramify -type=User
type User struct {
ID string `json:"id"`
Name string `json:"name"`
Age uint `json:"age,omitempty"`
Address *Address `json:"address,omitempty"`
Friends []string `json:"friends,omitempty"`
}
type Address struct {
Street string `json:"street"`
}
func main() {
john := NewUser(
"1", // Required: ID
"John", // Required: Name
)
sam := NewUser(
"2", // Required: ID
"Sam", // Required: Name
WithUserAge(20), // Optional: Age
WithUserAddress(Address{"street"}), // Optional: Address
WithUserFriends([]string{"Jane"}), // Optional: Friends
)
fmt.Printf("%+v\n", john)
fmt.Printf("%+v\n", sam)
}
```
## Contributing
We welcome contributions to this project! To get started, please refer to our [Contribution Guide](https://github.com/yukinagae/paramify/blob/main/CONTRIBUTING.md).