Skip to content

Commit

Permalink
Merge pull request #265 from longit644/master
Browse files Browse the repository at this point in the history
v2: Replace parallel arrays with array of objects
  • Loading branch information
k8s-ci-robot authored Mar 10, 2024
2 parents 51d4e06 + 2a31dd7 commit 9cff633
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
16 changes: 8 additions & 8 deletions v2/namer/namer.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,12 +281,12 @@ func (ns *NameStrategy) Name(t *types.Type) string {
case types.Func:
// TODO: add to name test
parts := []string{"Func"}
for _, pt := range t.Signature.Parameters {
parts = append(parts, ns.removePrefixAndSuffix(ns.Name(pt)))
for _, param := range t.Signature.Parameters {
parts = append(parts, ns.removePrefixAndSuffix(ns.Name(param.Type)))
}
parts = append(parts, "Returns")
for _, rt := range t.Signature.Results {
parts = append(parts, ns.removePrefixAndSuffix(ns.Name(rt)))
for _, result := range t.Signature.Results {
parts = append(parts, ns.removePrefixAndSuffix(ns.Name(result.Type)))
}
name = ns.Join(ns.Prefix, parts, ns.Suffix)
default:
Expand Down Expand Up @@ -374,12 +374,12 @@ func (r *rawNamer) Name(t *types.Type) string {
case types.Func:
// TODO: add to name test
params := []string{}
for _, pt := range t.Signature.Parameters {
params = append(params, r.Name(pt))
for _, param := range t.Signature.Parameters {
params = append(params, r.Name(param.Type))
}
results := []string{}
for _, rt := range t.Signature.Results {
results = append(results, r.Name(rt))
for _, result := range t.Signature.Results {
results = append(results, r.Name(result.Type))
}
name = "func(" + strings.Join(params, ",") + ")"
if len(results) == 1 {
Expand Down
12 changes: 8 additions & 4 deletions v2/parser/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,12 +602,16 @@ func goNameToName(in string) types.Name {
func (p *Parser) convertSignature(u types.Universe, t *gotypes.Signature) *types.Signature {
signature := &types.Signature{}
for i := 0; i < t.Params().Len(); i++ {
signature.Parameters = append(signature.Parameters, p.walkType(u, nil, t.Params().At(i).Type()))
signature.ParameterNames = append(signature.ParameterNames, t.Params().At(i).Name())
signature.Parameters = append(signature.Parameters, &types.ParamResult{
Name: t.Params().At(i).Name(),
Type: p.walkType(u, nil, t.Params().At(i).Type()),
})
}
for i := 0; i < t.Results().Len(); i++ {
signature.Results = append(signature.Results, p.walkType(u, nil, t.Results().At(i).Type()))
signature.ResultNames = append(signature.ResultNames, t.Results().At(i).Name())
signature.Results = append(signature.Results, &types.ParamResult{
Name: t.Results().At(i).Name(),
Type: p.walkType(u, nil, t.Results().At(i).Type()),
})
}
if r := t.Recv(); r != nil {
signature.Receiver = p.walkType(u, nil, r.Type())
Expand Down
16 changes: 11 additions & 5 deletions v2/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,14 +423,20 @@ func (m Member) String() string {
return m.Name + " " + m.Type.String()
}

// ParamResult represents a parameter or a result of a method's signature.
type ParamResult struct {
// The name of the parameter or result.
Name string
// The type of this parameter or result.
Type *Type
}

// Signature is a function's signature.
type Signature struct {
// If a method of some type, this is the type it's a member of.
Receiver *Type
Parameters []*Type
ParameterNames []string
Results []*Type
ResultNames []string
Receiver *Type
Parameters []*ParamResult
Results []*ParamResult

// True if the last in parameter is of the form ...T.
Variadic bool
Expand Down

0 comments on commit 9cff633

Please sign in to comment.