# README
ast fields flat
主要功能是把分组的元素展开为平铺的
简单举例说明,在go语言代码中,函数的参数定义可能是这样的:
(a, b int, c, d Axx)
很明显这样的类型非常不利于使用,这是因为ast语法分析以后会得到:
astFields []*ast.Field
在本例中会有两个 *ast.Field,按照类型分别是 int
和 Axx
的,他们分别有各自的 Names []*Ident
元素列表:
在 int
Field 里有 a
和 b
两个元素
在 Axx
Field 里有 c
和 d
两个元素
这样想做事情的时候就会略微麻烦些 倒不如把它们转化为平铺的结构,即
(a int, b int, c Axx, d Axx)
这样我们就很清楚有几个参数,这些参数的名称和类型都是很清楚的
这就是所谓的展开为平铺的
同时还做了些其它额外的操作,比如遇到匿名参数或者匿名返回值的时候,比如返回
(int, error)
这个列表的时候,我们当然希望能让数据结构是统一的,因此当把这个展开为平铺结构时,会把匿名的元素补个名称,比如:
(res int, err error)
这样完成结构化以后将会使得后面的操作相对简单些。
当然顺带还有个操作是,在包内定义的 Axx
类型在包外使用时就得使用 pkg.Axx
这样的类型,代码里有这个转换的逻辑。
但整体来说这个包的主要功能就是,把分组的参数列表转换为平铺的。
# Functions
ExtractNameTypeElements extracts NameTypeElements from the AST fields.
GetFuncGenericTypeParamsMap extracts a map of custom generic type parameter names and their associated types from a function declaration's type parameters.
GetGenericFuncTypeParamsMap extracts a map of custom generic type parameter names and their associated types from a function type's type parameters.
GetGenericTypeParamsMap extracts a map of custom generic type parameter names and their associated types from a type parameter field list.
GetSimpleArgElements extracts NameTypeElements from the given fields with an "arg" prefix for names.
GetSimpleResElements extracts NameTypeElements from the given fields with a "res" prefix for names.
MakePrefixedNameFunction returns a function that generates unique names with a prefix and index.
NewNameTypeElement creates a new NameTypeElement by parsing the source code for parameter/return information and normalizing the data for further code generation.
NewNameTypeElements creates a list of NameTypeElements based on AST field list.
NewPrefixedNameTypeElements creates a NameTypeElements instance using a naming function that adds a prefix and an index.
SimpleMakeNameFunction returns a function that generates names with a specified prefix, handling both normal and error cases.
# Structs
NameTypeElement represents a single element with a name, type, and associated information.
# Type aliases
MakeNameFunction generates a name for a parameter or return value.
NameTypeElements is a collection of NameTypeElement.
StatementLines represents a list of strings that are usually separated by newline characters.
StatementParts represents a list of strings that are usually separated by commas.