discriminatedunion

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2022 License: MIT Imports: 7 Imported by: 0

README

discriminatedunion

Check exhaustiveness of switch statements against discriminated unions.

go install github.com/thematthopkins/discriminatedunion/cmd/discriminatedunion@latest

Go frequently uses interfaces to signify discriminated unions. One bit that is lacking though is it does not have a way of enforcing that switches exhaustively handle all members of a discriminated union. This analyzer performs that check, so that if you add a new member to a discriminated union, you can have confidence that all type switches will be forced to account for the new member.

Example

Given a discriminated union defined by:

package shape

type Circle struct {
	float32 Radius
}

type Square struct {
	float32 Width
}

type Arc struct {
	float32 Radius
	float32 Radians
}

type Shape interface {
    // IsShape takes the form Is<interface>, which tells the discriminatedunion
    // checker that it is a discriminated union.
	IsShape()
}

// The receivers below signify which structs are members of the Shape discriminated union
func (Circle) IsShape() {}
func (Square) IsShape() {}
func (Arc) IsShape()    {}

with the code below:

package shapeprinter

import (
    "fmt"
    "shape"
)

func printShape(s shape.Shape) {
	switch s := s {
	case shape.Circle:
        fmt.Printf("Circle Radius %v\n", s.Radius)
	case shape.Square:
        fmt.Printf("Square Width %v\n", s.Width)
	}
}

running discriminatedunion ./..., or adding it to your go/analysis/multichecker will print:

shapeprinter.go:14:3: missing cases for discriminated union types: shape.Arc

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Analyzer = &analysis.Analyzer{
	Name:      "discriminated",
	Doc:       "check exhaustive pattern matching for discriminated unions",
	Run:       run,
	Requires:  []*analysis.Analyzer{inspect.Analyzer},
	FactTypes: []analysis.Fact{&DiscriminatedUnionFact{}},
}

Functions

This section is empty.

Types

type DiscriminatedUnionFact

type DiscriminatedUnionFact struct {
	Types []string
}

func (*DiscriminatedUnionFact) AFact

func (f *DiscriminatedUnionFact) AFact()

func (*DiscriminatedUnionFact) String

func (f *DiscriminatedUnionFact) String() string

type NameWithInterface

type NameWithInterface struct {
	Name          *ast.Ident
	Interfaze     *ast.InterfaceType
	SignifierFunc *ast.FuncType
}

Directories

Path Synopsis
cmd
discriminatedunion
Exhaustiveness checks for discriminated unions.
Exhaustiveness checks for discriminated unions.

Jump to

Keyboard shortcuts

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