![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
github.com/searKing/golang/tools/go-option
Generates Go code using a package as a graceful option.
go-option Generates Go code using a package as a graceful options Given the name of an atomic.Value type T go-option will create a new self-contained Go source file implementing
// type TOption interface{
apply(*Number)
}
// T
func (m *T) ApplyOptions(options ...TOption) *T
The file is created in the same package and directory as the package that defines T. It has helpful defaults designed for use with go generate.
For example, given this snippet,
package painkiller
//go:generate go-option -type "Pill"
type Pill[T comparable] struct {
// This is Name doc comment
Name string // This is Name line comment
Age string `option:",short"`
Address string `option:"-"`
NameAlias string `option:"Title,"`
arrayType [5]T
funcType func()
interfaceType interface{}
mapType map[string]int
sliceType []int64
}
running this command
go-option -type "Pill"
in the same directory will create the file pill_options.go, in package painkiller, containing a definition of
// Code generated by "go-option -type Pill"; DO NOT EDIT.
// Install go-option by "go get install github.com/searKing/golang/tools/go-option"
package painkiller
// A PillOption sets options.
type PillOption[T comparable] interface {
apply(*Pill[T])
}
// EmptyPillOption does not alter the configuration. It can be embedded
// in another structure to build custom options.
//
// This API is EXPERIMENTAL.
type EmptyPillOption[T comparable] struct{}
func (EmptyPillOption[T]) apply(*Pill[T]) {}
// PillOptionFunc wraps a function that modifies Pill[T] into an
// implementation of the PillOption[T comparable] interface.
type PillOptionFunc[T comparable] func(*Pill[T])
func (f PillOptionFunc[T]) apply(do *Pill[T]) {
f(do)
}
// ApplyOptions call apply() for all options one by one
func (o *Pill[T]) ApplyOptions(options ...PillOption[T]) *Pill[T] {
for _, opt := range options {
if opt == nil {
continue
}
opt.apply(o)
}
return o
}
// WithPillName sets Name in Pill[T].
// This is Name doc comment
// This is Name line comment
func WithPillName[T comparable](v string) PillOption[T] {
return PillOptionFunc[T](func(o *Pill[T]) {
o.Name = v
})
}
// WithAge sets Age in Pill[T].
func WithAge[T comparable](v string) PillOption[T] {
return PillOptionFunc[T](func(o *Pill[T]) {
o.Age = v
})
}
// WithPillTitle sets NameAlias in Pill[T].
func WithPillTitle[T comparable](v string) PillOption[T] {
return PillOptionFunc[T](func(o *Pill[T]) {
o.NameAlias = v
})
}
// WithPillArrayType sets arrayType in Pill[T].
func WithPillArrayType[T comparable](v [5]T) PillOption[T] {
return PillOptionFunc[T](func(o *Pill[T]) {
o.arrayType = v
})
}
// WithPillInterfaceType sets interfaceType in Pill[T].
func WithPillInterfaceType[T comparable](v interface{}) PillOption[T] {
return PillOptionFunc[T](func(o *Pill[T]) {
o.interfaceType = v
})
}
// WithPillMapType appends mapType in Pill[T].
func WithPillMapType[T comparable](m map[string]int) PillOption[T] {
return PillOptionFunc[T](func(o *Pill[T]) {
if o.mapType == nil {
o.mapType = m
return
}
for k, v := range m {
o.mapType[k] = v
}
})
}
// WithPillMapTypeReplace sets mapType in Pill[T].
func WithPillMapTypeReplace[T comparable](v map[string]int) PillOption[T] {
return PillOptionFunc[T](func(o *Pill[T]) {
o.mapType = v
})
}
// WithPillSliceType appends sliceType in Pill[T].
func WithPillSliceType[T comparable](v ...int64) PillOption[T] {
return PillOptionFunc[T](func(o *Pill[T]) {
o.sliceType = append(o.sliceType, v...)
})
}
// WithPillSliceTypeReplace sets sliceType in Pill[T].
func WithPillSliceTypeReplace[T comparable](v ...int64) PillOption[T] {
return PillOptionFunc[T](func(o *Pill[T]) {
o.sliceType = v
})
}
Typically, this process would be run using go generate, like this:
//go:generate go-option -type "Pill"
If multiple constants have the same value, the lexically first matching name will be used (in the example, Acetaminophen will print as "Paracetamol").
With no arguments, it processes the package in the current directory. Otherwise, the arguments must name a single directory holding a Go package or a set of Go source files that represent a single Go package.
The -type flag accepts a comma-separated list of types so a single run can generate methods for multiple types. The default output file is t_options.go, where t is the lower-cased name of the first type listed. It can be overridden with the -output flag.
The easiest way to install is to run go get install github.com/searKing/golang/tools/go-option
. You can also manually git clone the repository to $GOPATH/src/github.com/searKing/golang/tools/go-option
.
FAQs
Unknown package
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.