optgen

command module
v1.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 20, 2023 License: MIT Imports: 14 Imported by: 0

README

optgen

GoDoc Build Status codecov Go Report Card

Optional function generator from struct.

Limitation

  • Flag -w currently cannot update generated code, only append code

Installation

go install github.com/lumochift/optgen@latest

Usage

  • Help function
Usage of ./optgen:
  -all
        generate all fields
  -file string
        path file
  -name string
        struct name
  -tag string
        custom tag (default "opt")
  -w    enable write mode
  • Generate file with default tags opt
//thing.go
package foo

type Thing struct {
    Field1 string `opt` 
    Field2 []*int  `opt`
    Field3 map[byte]float64
}

For example we have thing.go and need to generate functional option for struct Thing, only field with opt will generated:

optgen -file thing.go -name Thing

Output:

// NewThing returns a *Thing.
func NewThing(opt ...func(*Thing)) *Thing {

        // Prepare a Thing 
        thing := &Thing{}

        // Apply options.
        for _, o := range opt {
                o(thing)
        }

        // Do anything here

        return thing
}


// SetField1 sets the Field1
func SetField1(field1 string) func(*Thing) {
        return func(c *Thing) {
                c.Field1 = field1
        }
}

// SetField2 sets the Field2
func SetField2(field2 []*int) func(*Thing) {
        return func(c *Thing) {
                c.Field2 = field2
        }
}

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
package generator provides functionality to generate functional option approach by given struct
package generator provides functionality to generate functional option approach by given struct

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL