gounion

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2024 License: MIT Imports: 7 Imported by: 0

README

Union Type in Go and Static Check Tool of switch-case handling

gounioncheck

Installation

$ go install github.com/avoronkov/gounion/cmd/gounioncheck@latest

Example

// main.go
package main

type Foo interface {
	foo()
}

type Bar struct{}

func (b Bar) foo() {}

type Baz struct{}

func (b *Baz) foo() {}

func main() {
	var foo Foo = &Bar{}
	switch foo.(type) {
	case *Bar:
		// Bar
	}
}
$ gounioncheck .
main.go:17:2: uncovered cases for main.Foo type switch: *main.Baz, main.Bar

Additional information

It cheks only so-called "private" interfaces i.e. interfaces with private methods which can be implemented only whithin the package they are declared.

Documentation

Overview

Package gosum provides utilities for Sum/Union/Variant like types.

Index

Constants

This section is empty.

Variables

View Source
var Analyzer = &analysis.Analyzer{
	Name: "gounioncheck",
	Doc:  "Static Check Tool of type switch-case handling",
	Run:  run,
}

Functions

func IsInternalInterface

func IsInternalInterface(iface *types.Interface) bool

IsInternalInterface returns true if the given interface has unexported method.

Types

type SumInterface

type SumInterface struct {
	// underling interface type. It uses types.Named instead of types.Interface
	// for convenience.
	NamedInterface *types.Named

	// is "internal interface" which includes unexported methods, otherwise
	// "public" interface.
	IsInternal bool

	// Implements holds types which implement SumInterface type.
	Implements struct {
		Interfaces []*SumInterface
		Pointers   []*types.Pointer
		Types      []*types.Type
	}

	// "Implements" only holds types in PkgScope. It's especially for "public"
	// interface.
	PkgScope []*types.Package
}

SumInterface represents Sum type by interface{} type.

func NewSumInterface

func NewSumInterface(namedInterface *types.Named, pkgscope []*types.Package) *SumInterface

NewSumInterface returns SumInterface from given interface type. If the given interface is "public", this function searches implemented types in pkgscope. pkgscope can be nil and it's useful to pass nil if you know the interface is "internal" interface.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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