analysis

package
v0.0.0-...-7625c6f Latest Latest
Warning

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

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

Documentation

Overview

Package analysis defines the types structures used by every output format. It may be seen as an extension of `go/types`.

Index

Constants

View Source
const (
	// ExhaustiveTypeSwitch may be used as marker to make futur refactor easier
	ExhaustiveTypeSwitch = "exhaustive analysis.Type type switch"

	// ExhaustiveAnonymousTypeSwitch may be used as marker to make futur refactor easier
	ExhaustiveAnonymousTypeSwitch = "exhaustive analysis.AnonymousType type switch"

	// ExhaustiveBasicKindSwitch may be used as marker to make futur refactor easier
	ExhaustiveBasicKindSwitch = "exhaustive analysis.BasicKind switch"
)
View Source
const IgnoreDeclComment = "gomacro:no-enum"

Variables

View Source
var (
	String = &Basic{B: types.Typ[types.String]}
	Bool   = &Basic{B: types.Typ[types.Bool]}
	Int    = &Basic{B: types.Typ[types.Int]}
	Float  = &Basic{B: types.Typ[types.Float64]}
)

Functions

func LoadSource

func LoadSource(sourceFile string) (*packages.Package, error)

LoadSource returns the `packages.Package` containing the given file.

func LoadSources

func LoadSources(sourceFiles []string) ([]*packages.Package, string, error)

LoadSources returns for each source file, the `*packages.Package` containing it. Since it only calls `packages.Load` once, it is a faster alternative to repeated `LoadSource` calls. It also returns the common (root) directory for all the files.

func LocalName

func LocalName(ty Type) string

LocalName returns the local name of the type. It will panic if `ty` is not named.

Types

type Analysis

type Analysis struct {
	// Types adds the additional analysis of this package,
	// and contains all the types needed by `Outline` and
	// their dependencies.
	Types map[types.Type]Type

	// Root is the root package used to query type information.
	Root *packages.Package

	// Source is the list of top-level types
	// defined in the analysis input file.
	Source []types.Type
}

Analysis is the result of analyzing one package.

func NewAnalysisFromFile

func NewAnalysisFromFile(pa *packages.Package, sourceFile string) *Analysis

NewAnalysisFromFile uses the given Package to build the analysis for the types defined in `sourceFile`.

func NewAnalysisFromTypes

func NewAnalysisFromTypes(root *packages.Package, source []types.Type) *Analysis

NewAnalysisFromTypes build the analysis for the given `types`. `root` is the root package, required to query type information.

func (*Analysis) GetByName

func (an *Analysis) GetByName(source *types.Named, name string) Type

GetByName returns the type [name], which must be in the same scope as [source]

type AnonymousType

type AnonymousType interface {
	Type
	// contains filtered or unexported methods
}

AnonymousType are the types which may be seen without associated names. Contrary to the Go language, this package does not support anonymous structs, enums and unions.

type Array

type Array struct {
	Elem Type
	// -1 for slices
	Len int
}

Array is either a fixed or variable length array

func (*Array) Type

func (ar *Array) Type() types.Type

type Basic

type Basic struct {
	B *types.Basic
}

Basic represents all simple types. Enums are special cased, meaning they are not of type `Basic`.

func (*Basic) Kind

func (b *Basic) Kind() BasicKind

func (*Basic) Type

func (b *Basic) Type() types.Type

type BasicKind

type BasicKind uint8

BasicKind is a simplified information of the kind of a basic type, typically shared by Dart, JSON and TypeScript generator

const (
	BKString BasicKind = iota
	BKInt
	BKFloat
	BKBool
)

func NewBasicKind

func NewBasicKind(info types.BasicInfo) (BasicKind, bool)

type CommentKind

type CommentKind uint8
const (

	// CommentSQL is used to add SQL statements to the generated tables creation code
	CommentSQL CommentKind
)

type Enum

type Enum struct {

	// Members contains all the values, even the unexported one
	Members []EnumMember

	// IsIota is `true` if the enum exported values are consecutive positive integer starting at zero.
	IsIota bool
	// contains filtered or unexported fields
}

func (*Enum) Get

func (e *Enum) Get(name string) EnumMember

Get return the enum value with Go name [name]. It panics if [name] is not found

func (*Enum) IsInteger

func (e *Enum) IsInteger() bool

IsInteger returns `true` is this enum is backed by integers (which may be negative and not contiguous)

func (*Enum) Type

func (e *Enum) Type() types.Type

func (*Enum) Underlying

func (e *Enum) Underlying() *types.Basic

Underlying returns the basic type used by this enum/

type EnumMember

type EnumMember struct {
	Const *types.Const
	// Comment is an optional comment associated with
	// the enum value.
	// If provided, it is used as label.
	Comment string
}

EnumMember decribe the value of one item in an enumeration type.

type Linker

type Linker struct {

	// Extension is added to all the output files returned
	// by the linker.
	Extension string
	// contains filtered or unexported fields
}

Linker is responsible for attributing the correct output file to a given type, recreating the package tree.

func NewLinker

func NewLinker(rootDirectory string, sources []*Analysis) Linker

func (Linker) GetOutput

func (lk Linker) GetOutput(ty types.Type) string

GetOutput returns the file where [ty] should be defined, adding [Extension]

func (Linker) IsPredefined

func (lk Linker) IsPredefined(outFile string) bool

func (Linker) OutputFiles

func (lk Linker) OutputFiles() []string

OutputFiles returns a list of file names, adding [Extension]

type Map

type Map struct {
	Key  Type
	Elem Type
}

func (*Map) Type

func (ma *Map) Type() types.Type

type Named

type Named struct {
	Underlying AnonymousType
	// contains filtered or unexported fields
}

Named is a named type pointing to an `AnonymousType`. Structs, enums, and unions are NOT `Named`

func (*Named) Type

func (na *Named) Type() types.Type

type PkgSelector

type PkgSelector struct {
	// contains filtered or unexported fields
}

PkgSelector allows to retrict the packages import graph walk to the user written.

func NewPkgSelector

func NewPkgSelector(root *packages.Package) PkgSelector

func (PkgSelector) Ignore

func (ps PkgSelector) Ignore(pa *packages.Package) bool

Ignore returns true if the given package should not be recursed on.

type Pointer

type Pointer struct {
	Elem Type
}

Pointer is a pointer to a type

func (*Pointer) Type

func (p *Pointer) Type() types.Type

type SpecialComment

type SpecialComment struct {
	Content string
	Kind    CommentKind
}

SpecialComment is a comment found on a struct declaration. See `CommentKind` for the supported comments.

type Struct

type Struct struct {
	Name *types.Named

	Fields     []StructField
	Comments   []SpecialComment
	Implements []*Union
}

func (*Struct) Type

func (cl *Struct) Type() types.Type

type StructField

type StructField struct {
	Type  Type
	Field *types.Var        // returned by Struct.Field()
	Tag   reflect.StructTag // returned by Struct.Tag()
}

func (StructField) Exported

func (st StructField) Exported() bool

Exported returns `true` is the field is exported and should be included in the generated code. Ignored field are either :

  • unexported
  • with a 'json' tag '-'
  • with a 'gomacro' tag 'ignore'

func (StructField) IsOpaqueFor

func (st StructField) IsOpaqueFor(target string) bool

IsOpaqueFor returns true if the field should be considered as dynamic when generating code for [target]

func (StructField) JSONName

func (st StructField) JSONName() string

JSONName returns the field name used by Go json package, that is, taking into account the json struct tag.

type Time

type Time struct {
	// IsDate is true if only year/month/day are actually
	// of interest for this type.
	// The following heuristic is used to compute it :
	//	- a type containing Date in its name (case insensitive match) will have IsDate = true
	IsDate bool
}

Time is a special case for *time.Time, which is not handled like a regular "pointer to named struct"

func (*Time) Type

func (ti *Time) Type() types.Type

type Type

type Type interface {
	// Type returns the Go type corresponding to this tag.
	Type() types.Type
}

Type is the common interface for all the supported types. All implementation are pointers, so that they can easily be mapped to output types.

func NewTime

func NewTime(typ types.Type) (Type, bool)

NewTime returns `true` is the underlying type of `typ` is time.Time

type Union

type Union struct {

	// The types implementing this interface, sorted by name.
	// By construction, their Type() method will always return a *types.Named,
	// and Obj().Name() should be used as an identifier tag
	// accros the generators.
	Members []Type
	// contains filtered or unexported fields
}

Union is deduced from interfaces, with the limitation that only types defined in the same package are considered as members of the union

func (*Union) IsExported

func (u *Union) IsExported() bool

IsExported returns `true` is the interface is exported.

func (*Union) Type

func (u *Union) Type() types.Type

Type always return a *types.Named

Directories

Path Synopsis
test/echo
Package echo is a substitute for the http framework echo package.
Package echo is a substitute for the http framework echo package.
test/inner
Package inner is only used to test apigen.
Package inner is only used to test apigen.
sql
Package sql implements the logic required to analyze the links between SQL tables (represented as structs in the Go source code).
Package sql implements the logic required to analyze the links between SQL tables (represented as structs in the Go source code).
test/pq
Package pq is used as replacement for "github.com/lib/pq" in tests
Package pq is used as replacement for "github.com/lib/pq" in tests

Jump to

Keyboard shortcuts

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