stringenumer

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2022 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package stringenumer generate useful code for enums defined as strings

type MyEnum string
const (
	MyEnumThis MyEnum = "this"
	MyEnumThat MyEnum = "that"
)

The code that is generated by default is a Valid function:

func (e MyEnum) Valid() bool

It can also generate an TextUnmarshaling function for the type that validates any string that is unmarshaled into this type. Via, for example, json.Unmarshal.

Example
package main

import (
	"io"
	"log"
	"os"

	"github.com/lindell/string-enumer/pkg/stringenumer"
)

func main() {
	r, err := stringenumer.Generate(
		stringenumer.Paths("."),
		stringenumer.TypeNames("MyType", "YourType"),
		stringenumer.TextUnmarshaling(true),
	)
	if err != nil {
		log.Fatalln(err)
	}
	_, _ = io.Copy(os.Stdout, r)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Generate

func Generate(options ...Option) (io.Reader, error)

Generate returns a reader with generated code

Types

type Option

type Option func(*generator)

Option is used in the call of Generate to change the behavior

func Paths

func Paths(paths ...string) Option

Paths sets the paths from where code should be read from

func TextUnmarshaling

func TextUnmarshaling(unmarshalText bool) Option

TextUnmarshaling sets if text unarshaling should be generated or not

func TypeNames

func TypeNames(types ...string) Option

TypeNames is the options used to set the types that code should be generated for At least one type needs to be defined

Jump to

Keyboard shortcuts

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