common

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: May 29, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package common holds structalign's public contracts: the interfaces that decouple the CLI's layers and the plain data types those interfaces traffic in. Implementations live under internal/. Keeping the contracts here (rather than internal/) lets mockery generate mocks from a non-internal source.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ColorizeStrings added in v0.6.0

func ColorizeStrings() []string

ColorizeStrings returns a slice of all String values of the enum

func DiffStyleStrings

func DiffStyleStrings() []string

DiffStyleStrings returns a slice of all String values of the enum

func FormatStrings added in v0.7.0

func FormatStrings() []string

FormatStrings returns a slice of all String values of the enum

Types

type Aligner

type Aligner interface {
	Findings(t Target, opts Options) ([]Finding, error)
}

Aligner produces the struct-reordering findings for one Target, controlled by opts.

type Colorize added in v0.6.0

type Colorize uint8

Colorize selects when colored output is emitted. enumer generates its String/parse/text-marshal helpers, a flag.Value implementation (Set), and a Type method for usage strings, see colorize_enumer.go; the names map to "auto"/"always"/"never" via -trimprefix=Colorize -transform=lower.

const (
	ColorizeAuto   Colorize = iota // color only on a TTY with NO_COLOR unset
	ColorizeAlways                 // always color, overriding NO_COLOR
	ColorizeNever                  // never color
)

func ColorizeString added in v0.6.0

func ColorizeString(s string) (Colorize, error)

ColorizeString retrieves an enum value from the enum constants string name. Throws an error if the param is not part of the enum.

func ColorizeValues added in v0.6.0

func ColorizeValues() []Colorize

ColorizeValues returns all values of the enum

func (Colorize) IsAColorize added in v0.6.0

func (i Colorize) IsAColorize() bool

IsAColorize returns "true" if the value is listed in the enum definition. "false" otherwise

func (Colorize) MarshalText added in v0.6.0

func (i Colorize) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface for Colorize

func (*Colorize) Set added in v0.6.0

func (i *Colorize) Set(value string) error

Set allows flag and pflag libraries to set a value dynamically.

func (Colorize) String added in v0.6.0

func (i Colorize) String() string

func (Colorize) Type added in v0.6.0

func (Colorize) Type() string

Type returns a string that represents all possible values to this type joined by '|'.

func (*Colorize) UnmarshalText added in v0.6.0

func (i *Colorize) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface for Colorize

type DiffStyle

type DiffStyle uint8

DiffStyle selects how a Finding is rendered. enumer generates its String/parse/text-marshal helpers, a flag.Value implementation (Set), and a Type method for usage strings, see diffstyle_enumer.go; the names map to "unified"/"side"/"none" via -trimprefix=Diff -transform=lower.

const (
	DiffUnified DiffStyle = iota
	DiffSide
	DiffNone
)

func DiffStyleString

func DiffStyleString(s string) (DiffStyle, error)

DiffStyleString retrieves an enum value from the enum constants string name. Throws an error if the param is not part of the enum.

func DiffStyleValues

func DiffStyleValues() []DiffStyle

DiffStyleValues returns all values of the enum

func (DiffStyle) IsADiffStyle

func (i DiffStyle) IsADiffStyle() bool

IsADiffStyle returns "true" if the value is listed in the enum definition. "false" otherwise

func (DiffStyle) MarshalText

func (i DiffStyle) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface for DiffStyle

func (*DiffStyle) Set

func (i *DiffStyle) Set(value string) error

Set allows flag and pflag libraries to set a value dynamically.

func (DiffStyle) String

func (i DiffStyle) String() string

func (DiffStyle) Type added in v0.6.0

func (DiffStyle) Type() string

Type returns a string that represents all possible values to this type joined by '|'.

func (*DiffStyle) UnmarshalText

func (i *DiffStyle) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface for DiffStyle

type Finding

type Finding struct {
	Package    string
	Fset       *token.FileSet
	Pos        token.Pos
	Name       string // enclosing named type, or "" for an anonymous struct
	TypeParams string // type-parameter names for a generic type, e.g. "[T]" (else "")
	Message    string // analyzer diagnostic (carries the size info)
	Original   string // current struct source ("struct{...}")
	Proposed   string // reordered struct from the analyzer's SuggestedFix
	OldSize    int64  // current struct size from the analyzer message (0 if unknown)
	NewSize    int64  // proposed (optimal) size from the analyzer message (0 if unknown)
}

Finding is one struct whose fields could be reordered to use less memory.

type Format added in v0.7.0

type Format uint8

Format selects the output presentation style. enumer generates its String/parse/text-marshal helpers, a flag.Value implementation (Set), and a Type method for usage strings, see format_enumer.go; the names map to "text"/"json" via -trimprefix=Format -transform=lower.

const (
	FormatText Format = iota
	FormatJSON
)

func FormatString added in v0.7.0

func FormatString(s string) (Format, error)

FormatString retrieves an enum value from the enum constants string name. Throws an error if the param is not part of the enum.

func FormatValues added in v0.7.0

func FormatValues() []Format

FormatValues returns all values of the enum

func (Format) IsAFormat added in v0.7.0

func (i Format) IsAFormat() bool

IsAFormat returns "true" if the value is listed in the enum definition. "false" otherwise

func (Format) MarshalText added in v0.7.0

func (i Format) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface for Format

func (*Format) Set added in v0.7.0

func (i *Format) Set(value string) error

Set allows flag and pflag libraries to set a value dynamically.

func (Format) String added in v0.7.0

func (i Format) String() string

func (Format) Type added in v0.7.0

func (Format) Type() string

Type returns a string that represents all possible values to this type joined by '|'.

func (*Format) UnmarshalText added in v0.7.0

func (i *Format) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface for Format

type Inspector

type Inspector interface {
	Layouts(t Target, opts Options) []Layout
}

Inspector computes the memory layout of each named struct in a Target, controlled by opts.

type Layout

type Layout struct {
	Package    string
	Name       string
	TypeParams string // for a generic type, e.g. "[T]" (else "")
	Note       string // optional caveat shown above the struct (e.g. the generic disclaimer)
	Total      int64
	Align      int64
	Padding    int64 // total padding across all fields
	Fields     []LayoutField
}

Layout is one named struct's computed memory layout.

type LayoutField

type LayoutField struct {
	Name    string
	Type    string
	Tag     string // raw struct tag without backticks, or ""
	Assume  string // for a generic field, the assumed type param(s), e.g. "T=any" (else "")
	Offset  int64
	Size    int64
	Align   int64
	Padding int64 // padding inserted after this field
}

LayoutField is one field's place in a struct's memory layout.

type Loader

type Loader interface {
	Load(patterns ...string) ([]Target, error)
}

Loader resolves Go package patterns (./..., import paths, directories, and "file=" queries) into typed Targets.

type Options added in v0.4.0

type Options struct {
	Patterns         []string // -type globs matched against named-type names (nil = all)
	KeepTags         bool     // preserve struct field tags in rendered text
	IncludeGenerated bool     // analyze structs in generated files (default: skip them)
	SkipCachePadded  bool     // skip structs containing a golang.org/x/sys/cpu.CacheLinePad field
	RespectNolint    bool     // suppress findings on types carrying a recognized //nolint directive
	NolintLinters    []string // named //nolint tokens that trigger suppression (bare //nolint always counts)
}

Options controls which structs are analyzed and how findings are produced.

type Sizes

type Sizes interface {
	Sizeof(t types.Type) int64
	Alignof(t types.Type) int64
	Offsetsof(fields []*types.Var) []int64
}

Sizes abstracts go/types sizing so the target architecture is injectable: a real types.Sizes in production, a fixed types.SizesFor("gc","amd64") in tests (making golden output deterministic on any host arch). Its method set matches go/types.Sizes, so a common.Sizes value is directly assignable to a types.Sizes (e.g. analysis.Pass.TypesSizes).

type Target

type Target struct {
	PkgPath   string
	Fset      *token.FileSet
	Syntax    []*ast.File
	Types     *types.Package
	TypesInfo *types.Info
	Sizes     Sizes
	Errors    []error
}

Target is a loader-agnostic view of one typed Go package: everything the analyzer and the layout inspector need, without exposing go/packages.Package.

Jump to

Keyboard shortcuts

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