enum

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: MIT Imports: 0 Imported by: 0

README

go-enum

Idea from following video.
列挙型の作り方を再考する - Go Conference 2023 Online

This package provides support for defining Visitor and implementing Accept() methods.

Usage

Examples here.

  1. Prepare enum interface and member types.
package fruits

import "github.com/daichitakahashi/go-enum"

type (
	// enum interface
	Fruits interface{}

	// member types
	Apple  enum.MemberOf[Fruits]
	Orange enum.MemberOf[Fruits]
	Grape  enum.MemberOf[Fruits]
)
  1. Run following command.
$ go run github.com/daichitakahashi/go-enum/cmd/enumgen
  1. Following code is generated with the file name enum.gen.go(file name is configurable).
// Code generated by enumgen. DO NOT EDIT.

package fruits

type (
	FruitsVisitor interface {
		VisitApple(e Apple)
		VisitOrange(e Orange)
		VisitGrape(e Grape)
	}
	FruitsEnum interface {
		Accept(v FruitsVisitor)
	}
)

func (e Apple) Accept(v FruitsVisitor) {
	v.VisitApple(e)
}
func (e Orange) Accept(v FruitsVisitor) {
	v.VisitOrange(e)
}
func (e Grape) Accept(v FruitsVisitor) {
	v.VisitGrape(e)
}

var _ = []FruitsEnum{Apple{}, Orange{}, Grape{}}
  1. Optional: Embed generated FruitsEnum type in Fruits type.
type (
	// enum interface
	Fruits interface{
		FruitsEnum
	}
...
  1. Implement your visitor type!

Options for enumgen

option description default value
--wd working directory .
--out output file name enum.gen.go
--visitor customize Visitor type & method names *:*Visitor:Visit*
--accept customize Accept method name *:Accept
--visitor option

The value of --visitor option consists of three parts with the delimiter ":".

  1. The target type name(enum interface) of customization.
    Pattern match using * is allowed.
  2. The visitor type name pattern.
    If the pattern contains *, it will replaced with the target type name.
  3. The visit method name pattern.
    If the pattern contains *, it will replaced with the member type name.
--accept option

The value of --accept option consists of two parts with the delimiter ":"

  1. The target type name(enum interface) of customization.
    Pattern match using * is allowed.
  2. The accept method name pattern.
    If the pattern contains *, it will replaced with the target type name.

--visitor and --accept options can be used multiple times.

Example: use enumgen for domain events.

package event

import (
	"time"

	"github.com/daichitakahashi/go-enum"
)

//go:generate go run github.com/daichitakahashi/go-enum/cmd/enumgen@latest --visitor="Event:EventHandler:On*" --accept="Event:Emit"

type (
	Event interface {
		ID() string
	}

	OrderPlaced struct {
		enum.MemberOf[Event]
		Items []string
	}

	PaymentReceived struct {
		enum.MemberOf[Event]
		Amount int
	}

	ItemShipped struct {
		enum.MemberOf[Event]
		ShippedAt time.Time
	}
)

// ID implements Event.
func (OrderPlaced) ID() string {
	return "orderPlaced"
}

// ID implements Event.
func (PaymentReceived) ID() string {
	return "paymentReceived"
}

// ID implements Event.
func (ItemShipped) ID() string {
	return "itemShipped"
}

var (
	_ Event = OrderPlaced{}
	_ Event = PaymentReceived{}
	_ Event = ItemShipped{}
)

go generated:

// Code generated by enumgen. DO NOT EDIT.

package event

type (
	EventHandler interface {
		OnOrderPlaced(e OrderPlaced)
		OnPaymentReceived(e PaymentReceived)
		OnItemShipped(e ItemShipped)
	}
	EventEnum interface {
		Emit(v EventHandler)
	}
)

func (e OrderPlaced) Emit(v EventHandler) {
	v.OnOrderPlaced(e)
}
func (e PaymentReceived) Emit(v EventHandler) {
	v.OnPaymentReceived(e)
}
func (e ItemShipped) Emit(v EventHandler) {
	v.OnItemShipped(e)
}

var _ = []EventEnum{OrderPlaced{}, PaymentReceived{}, ItemShipped{}}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MemberOf

type MemberOf[EnumIdent any] struct{}

MemberOf marks membership of EnumIdent on struct.

type VisitorReturns added in v0.0.2

type VisitorReturns[Return any] interface{}

VisitorReturns specifies return type of visitor on enum identifier.

Directories

Path Synopsis
cmd
example

Jump to

Keyboard shortcuts

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