Documentation ¶
Index ¶
- func Generate(c Config, w io.Writer) error
- func GenerateString(c Config) (string, error)
- type Config
- func (c Config) ArgumentNames() []string
- func (c Config) ArgumentTypesDeduped() []string
- func (c Config) ArgumentsCalling() string
- func (c Config) ArgumentsNamed() (string, error)
- func (c Config) ArgumentsUnnamed() string
- func (c Config) Filename() string
- func (c Config) FuncTypeName() string
- func (c Config) ReturnSignature() string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Generate ¶
Generate generates code for the given type Config and writes it to the given Writer.
func GenerateString ¶
GenerateString returns code for the given type Config.
Types ¶
type Config ¶
type Config struct { Package string `json:"package"` Documentation string `json:"doc"` TypeName string `json:"type"` MethodName string `json:"method"` ArgumentTypes []string `json:"argument_types"` ReturnTypes []string `json:"return"` OutputFilename string `json:"filename"` }
Config encapsulates parameters for code generation.
func LoadConfigFromFile ¶
LoadConfigFromFile loads configuration from filename in JSON format.
func (Config) ArgumentNames ¶
ArgumentNames returns the names of the arguments.
Example ¶
package main import ( "fmt" "github.com/mmcloughlin/adorn" ) func main() { cfg := adorn.Config{ ArgumentTypes: []string{"int", "string", "string"}, } fmt.Println(cfg.ArgumentNames()) }
Output: [a0 a1 a2]
func (Config) ArgumentTypesDeduped ¶
ArgumentTypesDeduped returns the list of argument types, where runs of the same types are collapsed.
Example ¶
package main import ( "fmt" "strings" "github.com/mmcloughlin/adorn" ) func main() { cfg := adorn.Config{ ArgumentTypes: []string{"int", "int", "string", "string", "string"}, } fmt.Println(strings.Join(cfg.ArgumentTypesDeduped(), ",")) }
Output: ,int,,,string
func (Config) ArgumentsCalling ¶
ArgumentsCalling returns the comma separated list of argument names, used when calling the function or method
func (Config) ArgumentsNamed ¶
ArgumentsNamed returns the arguments signature with names.
func (Config) ArgumentsUnnamed ¶
ArgumentsUnnamed returns the list of argument types joined with commas.
Example ¶
package main import ( "fmt" "github.com/mmcloughlin/adorn" ) func main() { cfg := adorn.Config{ ArgumentTypes: []string{"int", "int", "string", "string", "string"}, } fmt.Println(cfg.ArgumentsUnnamed()) }
Output: int, int, string, string, string
func (Config) Filename ¶
Filename returns the file path to the output. This is OutputFilename if specified, otherwise the snake case version of TypeName with the ".go" extension.
func (Config) FuncTypeName ¶
FuncTypeName returns the name of the plain function type that implements the interface. This will be TypeName with "Func" appended.
Example ¶
package main import ( "fmt" "github.com/mmcloughlin/adorn" ) func main() { cfg := adorn.Config{ TypeName: "Greeter", } fmt.Println(cfg.FuncTypeName()) }
Output: GreeterFunc
func (Config) ReturnSignature ¶
ReturnSignature returns the specification of the return type.
Example (Multiple) ¶
package main import ( "fmt" "github.com/mmcloughlin/adorn" ) func main() { cfg := adorn.Config{ ReturnTypes: []string{"string", "error"}, } fmt.Println(cfg.ReturnSignature()) }
Output: (string, error)
Example (Single) ¶
package main import ( "fmt" "github.com/mmcloughlin/adorn" ) func main() { cfg := adorn.Config{ ReturnTypes: []string{"int"}, } fmt.Println(cfg.ReturnSignature()) }
Output: int