Skip to content

Commit

Permalink
Handle Ident types for StarExpr operand (#7616)
Browse files Browse the repository at this point in the history
Switch on StarExpr's operand, avoiding a panic on unhandled types.
Include the expression in the type name for SelectorExpr.
Include the * for StarExpr in type declarations.
  • Loading branch information
jhendrixMSFT authored Feb 1, 2024
1 parent bfb78b3 commit 3271c65
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 20 deletions.
17 changes: 17 additions & 0 deletions src/go/cmd/api_view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,20 @@ func TestAliasDiagnostics(t *testing.T) {
}
}
}

func TestVars(t *testing.T) {
review, err := createReview(filepath.Clean("testdata/test_vars"))
require.NoError(t, err)
require.NotZero(t, review)
countSomeChoice := 0
hasHTTPClient := false
for i := range review.Tokens {
if review.Tokens[i].Value == "SomeChoice" && review.Tokens[i-1].Value == "*" {
countSomeChoice++
} else if review.Tokens[i].Value == "http.Client" && review.Tokens[i-1].Value == "*" {
hasHTTPClient = true
}
}
require.EqualValues(t, 2, countSomeChoice)
require.True(t, hasHTTPClient)
}
3 changes: 3 additions & 0 deletions src/go/cmd/testdata/test_vars/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module test_vars

go 1.18
15 changes: 15 additions & 0 deletions src/go/cmd/testdata/test_vars/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package testvars

import "net/http"

var Client *http.Client

type SomeChoice struct{}

var (
SomeChoice1 *SomeChoice = &SomeChoice{}
SomeChoice2 *SomeChoice = &SomeChoice{}
)
17 changes: 13 additions & 4 deletions src/go/cmd/token_makers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ package cmd
import (
"fmt"
"go/ast"
"golang.org/x/exp/slices"
"regexp"
"sort"
"strings"
"unicode"

"golang.org/x/exp/slices"
)

// exportedFieldRgx matches exported field names like "policy.ClientOptions", "Transport", and "GetToken(...)"
Expand Down Expand Up @@ -52,10 +53,18 @@ func NewDeclaration(pkg Pkg, vs *ast.ValueSpec, imports map[string]string) Decla
// const LogCredential log.Classification = "Credential"
decl.Type = pkg.translateType(x.Sel.Name, imports)
case *ast.StarExpr:
// var defaultHTTPClient *http.Client
decl.Type = pkg.translateType(x.X.(*ast.SelectorExpr).Sel.Name, imports)
switch xX := x.X.(type) {
case *ast.Ident:
// var SomeTypeValue *SomeType
decl.Type = pkg.translateType("*"+xX.Name, imports)
case *ast.SelectorExpr:
// var defaultHTTPClient *http.Client
decl.Type = pkg.translateType(fmt.Sprintf("*%s.%s", xX.X, xX.Sel.Name), imports)
default:
fmt.Printf("unhandled declaration type %T for %s\n", xX, pkg.getText(vs.Type.Pos(), vs.Type.End()))
}
default:
fmt.Println("unhandled constant type " + pkg.getText(vs.Type.Pos(), vs.Type.End()))
fmt.Println("unhandled declaration " + pkg.getText(vs.Type.Pos(), vs.Type.End()))
}
} else if len(vs.Values) == 1 {
switch t := vs.Values[0].(type) {
Expand Down
10 changes: 5 additions & 5 deletions src/go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ module apiviewgo
go 1.18

require (
github.com/spf13/cobra v1.4.0
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.7.0
golang.org/x/exp v0.0.0-20230213192124-5e25df0256eb
golang.org/x/mod v0.6.0
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a
golang.org/x/mod v0.14.0
)

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
22 changes: 11 additions & 11 deletions src/go/go.sum
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q=
github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/exp v0.0.0-20230213192124-5e25df0256eb h1:PaBZQdo+iSDyHT053FjUCgZQ/9uqVwPOcl7KSWhKn6w=
golang.org/x/exp v0.0.0-20230213192124-5e25df0256eb/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/mod v0.6.0 h1:b9gGHsz9/HhJ3HF5DHQytPpuwocVTChQJK3AvoLRD5I=
golang.org/x/mod v0.6.0/go.mod h1:4mET923SAdbXp2ki8ey+zGs1SLqsuM2Y0uvdZR/fUNI=
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a h1:Q8/wZp0KX97QFTc2ywcOE0YRjZPVIx+MXInMzdvQqcA=
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a/go.mod h1:idGWGoKP1toJGkd5/ig9ZLuPcZBC3ewk7SzmH0uou08=
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit 3271c65

Please sign in to comment.