closed

package module
v0.0.0-...-82c290f Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2018 License: BSD-2-Clause Imports: 9 Imported by: 0

Documentation

Overview

Package closed extracts closed types from a package.

A closed type is a type whose valid values is a subset of the domain of its underlying type.

It is impossible to extract all closed types, but this package attempts to extract all that can be heuristically identified by matching common coding conventions. However, false positives and negatives are still possible.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bitset

type Bitset struct {

	//Flags are the single bit labels in this Bitset.
	Flags [][]*types.Const
	//OrFlags are any multibit convienence labels.
	OrFlags [][]*types.Const
	// contains filtered or unexported fields
}

A Bitset.

func (*Bitset) Types

func (b *Bitset) Types() []*types.TypeName

type EmptySum

type EmptySum struct {

	//Nil is true if the nil value is legal.
	Nil bool
	//Members are valid types for this sum.
	Members []types.Type
	// contains filtered or unexported fields
}

EmptySum is a defined empty interface externally specified to contain only a finite number of types.

func (*EmptySum) Types

func (e *EmptySum) Types() []*types.TypeName

type Enum

type Enum struct {

	//NonZero is true if there is an explicit comment directive forbidding
	//zero from being a valid value.
	//It is meaningless if there is a label for the zero value.
	NonZero bool
	//Labels are the valid members of the enumeration.
	//If len(Labels[i]) > 1, then Labels[i][1:] are synonyms.
	//For example, given
	//	type Enum int
	//	const (
	//		A Enum = iota
	//		B
	//		C = B
	//	)
	//there would be two Labels.
	//The first would just be A and the second would be B and C.
	Labels [][]*types.Const
	// contains filtered or unexported fields
}

Enum is a set of const labels with a defined type that is not a bitset.

func (*Enum) Types

func (e *Enum) Types() []*types.TypeName

type Interface

type Interface struct {

	//NonNil is true if there is a comment directive marked this
	//type as not being able to contain nil.
	NonNil bool
	//Members are the types in the sum.
	Members []*TypeNamesAndType
	//FalseMembers are unexported zero-sized types with the appropriate tag
	//that are used to embed in the exported members.
	FalseMembers []*TypeNamesAndType
	//TagMethods is a list of the tag methods used by this sum.
	TagMethods []string
	// contains filtered or unexported fields
}

An Interface is an interface with at least one exported method. If it follows the conventional means of marking a sum type by an empty, nullary, unexported method tag, that will be recorded in TagMethods. If not it may not be a sum type or a closed type, but likely is.

func (*Interface) Types

func (i *Interface) Types() []*types.TypeName

type OptionalStruct

type OptionalStruct struct {
	Discriminant *types.Var
	Field        *types.Var
	// contains filtered or unexported fields
}

An OptionalStruct is a struct of the form

struct {
	set bool
	val T
}

where the field val is only valid if set is true.

func (*OptionalStruct) Types

func (o *OptionalStruct) Types() []*types.TypeName

type Type

type Type interface {
	//Types returns the TypeNames for this Type.
	//Types()[0] is always the name of the defined type,
	//and Types()[1:] are aliases to that type definied in the same package.
	Types() []*types.TypeName
	// contains filtered or unexported methods
}

A Type is one of the closed types recognized by this package.

func InPackage

func InPackage(fs *token.FileSet, files []*ast.File, pkg *types.Package) ([]Type, error)

InPackage extracts closed types from a given package.

All parameters are required.

The pkg is expected to be error free. If not, the results of InPackage may be incorrect or incomplete.

The files must be all the files used to parse pkg.

The fs must be the FileSet used to parse pkg.

type TypeNamesAndType

type TypeNamesAndType struct {
	TypeName []*types.TypeName
	Type     types.Type
}

TypeNamesAndType records the TypeName of a defined type (always TypeName[0]) and any aliases in the same package (TypeName[1:]).

The Type field is either identical to TypeName.Type() or types.NewPointer(TypeName.Type()), whichever satisfies the interface that this value is associated with.

Directories

Path Synopsis
cmds
closed-explorer
Command closed-explorer analyzes a package and prints its closed types to stdout.
Command closed-explorer analyzes a package and prints its closed types to stdout.
clvalid
Command clvalid generates a method or func to validate that the value of a closed type is legal.
Command clvalid generates a method or func to validate that the value of a closed type is legal.
fillswitch
Command fillswitch(1) populates missing cases in Go switches when the expression switched is a *closed.Enum, *closed.Interface, or *closed.EmptySum.
Command fillswitch(1) populates missing cases in Go switches when the expression switched is a *closed.Enum, *closed.Interface, or *closed.EmptySum.
fillswitch/internal/guess
Package guess was copied out of the source for golang.org/x/tools/cmd/guru
Package guess was copied out of the source for golang.org/x/tools/cmd/guru

Jump to

Keyboard shortcuts

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