syntaxgo_astnorm

package
v0.0.54 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2025 License: MIT Imports: 7 Imported by: 3

README

ast fields flat

主要功能是把分组的元素展开为平铺的

简单举例说明,在go语言代码中,函数的参数定义可能是这样的:

(a, b int, c, d Axx)

很明显这样的类型非常不利于使用,这是因为ast语法分析以后会得到:

astFields []*ast.Field

在本例中会有两个 *ast.Field,按照类型分别是 intAxx 的,他们分别有各自的 Names []*Ident 元素列表: 在 int Field 里有 ab 两个元素 在 Axx Field 里有 cd 两个元素

这样想做事情的时候就会略微麻烦些 倒不如把它们转化为平铺的结构,即

(a int, b int, c Axx, d Axx)

这样我们就很清楚有几个参数,这些参数的名称和类型都是很清楚的

这就是所谓的展开为平铺的

同时还做了些其它额外的操作,比如遇到匿名参数或者匿名返回值的时候,比如返回

(int, error)

这个列表的时候,我们当然希望能让数据结构是统一的,因此当把这个展开为平铺结构时,会把匿名的元素补个名称,比如:

(res int, err error)

这样完成结构化以后将会使得后面的操作相对简单些。

当然顺带还有个操作是,在包内定义的 Axx 类型在包外使用时就得使用 pkg.Axx 这样的类型,代码里有这个转换的逻辑。

但整体来说这个包的主要功能就是,把分组的参数列表转换为平铺的。

Documentation

Overview

Package syntaxgo_astnorm provides function signature processing utilities Extract and process function params and return values from AST Generate code for function calls, variable definitions, and type conversions

syntaxgo_astnorm 包提供函数签名处理工具 从 AST 提取和处理函数参数和返回值 生成函数调用、变量定义和类型转换的代码

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetFuncGenericTypeParamsMap

func GetFuncGenericTypeParamsMap(funcDecl *ast.FuncDecl) map[string]ast.Expr

GetFuncGenericTypeParamsMap extracts a map of custom generic type parameter names and their associated types from a function declaration's type parameters. GetFuncGenericTypeParamsMap 从函数声明 (FuncDecl) 的泛型类型参数列表中提取自定义泛型类型参数名称及其关联的类型,返回一个以名称为键、类型表达式 (ast.Expr) 为值的映射。

func GetGenericFuncTypeParamsMap

func GetGenericFuncTypeParamsMap(funcType *ast.FuncType) map[string]ast.Expr

GetGenericFuncTypeParamsMap extracts a map of custom generic type parameter names and their associated types from a function type's type parameters. GetGenericFuncTypeParamsMap 从函数类型 (FuncType) 的泛型类型参数列表中提取自定义泛型类型参数名称及其关联的类型,返回一个以名称为键、类型表达式 (ast.Expr) 为值的映射。

func GetGenericTypeParamsMap

func GetGenericTypeParamsMap(fields *ast.FieldList) map[string]ast.Expr

GetGenericTypeParamsMap extracts a map of custom generic type parameter names and their associated types from a type parameter field list. GetGenericTypeParamsMap 从泛型类型参数字段列表 (FieldList) 中提取自定义泛型类型参数名称及其关联的类型,返回一个以名称为键、类型表达式 (ast.Expr) 为值的映射。

Types

type MakeNameFunction

type MakeNameFunction func(name *ast.Ident, kind string, idx int, anonymousIdx int) string

MakeNameFunction generates a name when processing function params and return values. MakeNameFunction 用于为参数或返回值生成名称。

func MakePrefixedNameFunction

func MakePrefixedNameFunction(prefix string) MakeNameFunction

MakePrefixedNameFunction returns a function that generates unique names with a prefix and index. MakePrefixedNameFunction 返回一个通过前缀和序号生成唯一名称的函数。

func SimpleMakeNameFunction

func SimpleMakeNameFunction(prefix string) MakeNameFunction

SimpleMakeNameFunction returns a function that generates names with a specified prefix, handling both normal and error cases. SimpleMakeNameFunction 返回一个函数,该函数生成带有指定前缀的名称,处理正常和错误的情况。

type NameTypeElement

type NameTypeElement struct {
	Name       string   // Field name / 字段名称
	Kind       string   // Type description (e.g., int, string, A, utils.A, *B, *pkg.B) / 类型描述 (例如: int, string, A, utils.A, *B, *pkg.B)
	Type       ast.Expr // Go's type representation / Go 的类型表示
	IsEllipsis bool     // Indicates if the type is a variadic (e.g., ...int, ...string) / 是否是变参类型 (例如: ...int, ...string)
}

NameTypeElement represents a single element with a name, type, and associated information. NameTypeElement 代表一个包含名称、类型和相关信息的元素

func NewNameTypeElement

func NewNameTypeElement(
	field *ast.Field,
	paramName string,
	paramType string,
	isEllipsis bool,
	packageName string,
	genericTypeParams map[string]ast.Expr,
) *NameTypeElement

NewNameTypeElement creates a new NameTypeElement with parsed source code info and normalized data Parses param/return information and prepares it to use in code generation

NewNameTypeElement 创建新的 NameTypeElement,包含已解析的源代码信息和规范化数据 解析参数/返回信息并准备用于代码生成

func (*NameTypeElement) AdjustTypeWithPackage

func (element *NameTypeElement) AdjustTypeWithPackage(
	packageName string,
	genericTypeParams map[string]ast.Expr,
)

AdjustTypeWithPackage adjusts the type to include the package name if needed (when using outside types). AdjustTypeWithPackage 如果需要(对于外部类型),将类型调整为包含包名。

type NameTypeElements

type NameTypeElements []*NameTypeElement

NameTypeElements is a collection of NameTypeElement. NameTypeElements 是 NameTypeElement 的集合。

func ExtractNameTypeElements

func ExtractNameTypeElements(
	fields []*ast.Field,
	nameFunc MakeNameFunction,
	source []byte,
	packageName string,
	genericTypeParams map[string]ast.Expr,
) NameTypeElements

ExtractNameTypeElements extracts NameTypeElements from the AST fields. ExtractNameTypeElements 从 AST 字段中提取 NameTypeElements。

func GetSimpleArgElements

func GetSimpleArgElements(fields []*ast.Field, sourceCode []byte) NameTypeElements

GetSimpleArgElements extracts NameTypeElements from the given fields with an "arg" prefix for names. GetSimpleArgElements 从给定字段中提取 NameTypeElements,并为名称添加 "arg" 前缀。

func GetSimpleResElements

func GetSimpleResElements(fields []*ast.Field, sourceCode []byte) NameTypeElements

GetSimpleResElements extracts NameTypeElements from the given fields with a "res" prefix for names. GetSimpleResElements 从给定字段中提取 NameTypeElements,并为名称添加 "res" 前缀。

func NewNameTypeElements

func NewNameTypeElements(
	fieldList *ast.FieldList,
	nameFunc MakeNameFunction,
	source []byte,
	packageName string,
	genericTypeParams map[string]ast.Expr,
) NameTypeElements

NewNameTypeElements creates a list of NameTypeElements based on AST field list. NewNameTypeElements 根据 AST 字段列表创建 NameTypeElements 的列表。

func NewPrefixedNameTypeElements

func NewPrefixedNameTypeElements(
	fieldList *ast.FieldList,
	prefix string,
	sourceCode []byte,
	pkgName string,
	genericTypeParams map[string]ast.Expr,
) NameTypeElements

NewPrefixedNameTypeElements creates a NameTypeElements instance using a naming function that adds a prefix and an index. NewPrefixedNameTypeElements 使用添加前缀和序号的命名函数创建一个 NameTypeElements 实例。

func (NameTypeElements) FormatAddressableNames

func (elements NameTypeElements) FormatAddressableNames() StatementParts

FormatAddressableNames returns the names prefixed with "&" (addressable names). FormatAddressableNames 返回带有 "&" 前缀的名称(可寻址名称)。

func (NameTypeElements) FormatNamesWithKinds

func (elements NameTypeElements) FormatNamesWithKinds() StatementParts

FormatNamesWithKinds returns the names with associated types (e.g., "a int"). FormatNamesWithKinds 返回名称及其类型(例如:"a int")。

func (NameTypeElements) GenerateFunctionParams

func (elements NameTypeElements) GenerateFunctionParams() StatementParts

GenerateFunctionParams generates the function parameters list. GenerateFunctionParams 生成函数参数列表。

func (NameTypeElements) GenerateVarDefinitions

func (elements NameTypeElements) GenerateVarDefinitions() StatementLines

GenerateVarDefinitions generates variable definitions (e.g., "var a int"). GenerateVarDefinitions 生成变量定义(例如:"var a int")。

func (NameTypeElements) GroupVarsByKindToLines

func (elements NameTypeElements) GroupVarsByKindToLines() StatementLines

GroupVarsByKindToLines groups variables based on type and generates the definition lines. GroupVarsByKindToLines 按类型分组变量,并生成相应的定义行。

func (NameTypeElements) Kinds

func (elements NameTypeElements) Kinds() []string

Kinds returns a list of types of the elements. Kinds 返回元素类型的列表。

func (NameTypeElements) Names

func (elements NameTypeElements) Names() StatementParts

Names returns a list of names of the elements. Names 返回元素名称的列表。

type StatementLines

type StatementLines []string

StatementLines represents a list of strings separated by newlines Used in assignment statements, return statements, and function calls

StatementLines 表示以换行符分隔的字符串列表 用于赋值语句、返回语句和函数调用

func (StatementLines) MergeLines

func (stmts StatementLines) MergeLines() string

MergeLines joins the elements in StatementLines with a newline character, and returns the resulting string. MergeLines 将 StatementLines 中的元素用换行符连接,并返回结果字符串。

type StatementParts

type StatementParts []string

StatementParts represents a list of strings separated by commas Used in parameter lists, return value lists, and function arguments

StatementParts 表示以逗号分隔的字符串列表 用于参数列表、返回值列表和函数参数

func (StatementParts) MergeParts

func (stmts StatementParts) MergeParts() string

MergeParts joins the elements in StatementParts with a comma and a space, and returns the resulting string. MergeParts 将 StatementParts 中的元素用逗号和空格连接,并返回结果字符串。

Jump to

Keyboard shortcuts

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