adorn

package module
v0.0.0-...-cc499d2 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2020 License: MIT Imports: 10 Imported by: 0

README

adorn

Generate function and decorator types for Golang interfaces

go.dev Reference Build status

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Generate

func Generate(c Config, w io.Writer) error

Generate generates code for the given type Config and writes it to the given Writer.

func GenerateString

func GenerateString(c Config) (string, error)

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

func LoadConfigFromFile(filename string) (Config, error)

LoadConfigFromFile loads configuration from filename in JSON format.

func (Config) ArgumentNames

func (c Config) ArgumentNames() []string

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

func (c Config) ArgumentTypesDeduped() []string

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

func (c Config) ArgumentsCalling() string

ArgumentsCalling returns the comma separated list of argument names, used when calling the function or method

func (Config) ArgumentsNamed

func (c Config) ArgumentsNamed() (string, error)

ArgumentsNamed returns the arguments signature with names.

func (Config) ArgumentsUnnamed

func (c Config) ArgumentsUnnamed() string

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

func (c Config) Filename() string

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

func (c Config) FuncTypeName() string

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

func (c Config) ReturnSignature() string

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

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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