Inspired by this gem of an idea (click the image to go to the original comment):
<img src=https://i.imgur.com/QSF9e4f.png height=300>
go get github.com/vasilevp/aboriginal/cmd/aboriginal
aboriginal -i ᐸinput_fileᐳ -o ᐸoutput_fileᐳ
You can omit both -i and -o, in which case STDIN/STDOUT will be used.
Create main.go with the following contents:
//go:generate aboriginal -i main.go -o main.gen.go
package main
import "fmt"
type T interface{} // template argument placeholder
type OptionalᐸTᐳ struct {
Value T
Valid bool
}
func main() {
optInt := Optionalᐸintᐳ{
Value: 42,
Valid: true,
}
fmt.Printf("%T %+v\n", optInt, optInt)
optFloat := Optionalᐸfloat64ᐳ{
Value: 42.42,
Valid: true,
}
fmt.Printf("%T %+v\n", optFloat, optFloat)
}
Then run it by calling
go generate && go run *.go
You should get the following output:
main.Optionalᐸintᐳ {Value:42 Valid:true}
main.Optionalᐸfloat64ᐳ {Value:42.42 Valid:true}
The algorithm is fairly simple:
XᐸTᐳXᐸYᐳ, where Y is an arbitrary type, generate an implementation of XᐸTᐳ, replacing T with Y for all member types (verbatim)XᐸTᐳ, generate implementations for those as well, replacing XᐸTᐳ with XᐸYᐳ in receiver typeimports.Process() (the core of goimports) on the resulting file to fix any unused imports (necessary since all imports are copied verbatim from the original file)This project is a joke. Please, for the love of go, don't use in production.
Go
100.0%
Inspired by this gem of an idea (click the image to go to the original comment):
<img src=https://i.imgur.com/QSF9e4f.png height=300>
go get github.com/vasilevp/aboriginal/cmd/aboriginal
aboriginal -i ᐸinput_fileᐳ -o ᐸoutput_fileᐳ
You can omit both -i and -o, in which case STDIN/STDOUT will be used.
Create main.go with the following contents:
//go:generate aboriginal -i main.go -o main.gen.go
package main
import "fmt"
type T interface{} // template argument placeholder
type OptionalᐸTᐳ struct {
Value T
Valid bool
}
func main() {
optInt := Optionalᐸintᐳ{
Value: 42,
Valid: true,
}
fmt.Printf("%T %+v\n", optInt, optInt)
optFloat := Optionalᐸfloat64ᐳ{
Value: 42.42,
Valid: true,
}
fmt.Printf("%T %+v\n", optFloat, optFloat)
}
Then run it by calling
go generate && go run *.go
You should get the following output:
main.Optionalᐸintᐳ {Value:42 Valid:true}
main.Optionalᐸfloat64ᐳ {Value:42.42 Valid:true}
The algorithm is fairly simple:
XᐸTᐳXᐸYᐳ, where Y is an arbitrary type, generate an implementation of XᐸTᐳ, replacing T with Y for all member types (verbatim)XᐸTᐳ, generate implementations for those as well, replacing XᐸTᐳ with XᐸYᐳ in receiver typeimports.Process() (the core of goimports) on the resulting file to fix any unused imports (necessary since all imports are copied verbatim from the original file)This project is a joke. Please, for the love of go, don't use in production.
Go
100.0%