Skip to content

Commit

Permalink
Merge pull request #22 from SiaFoundation/add-flags
Browse files Browse the repository at this point in the history
Add flag to check types
  • Loading branch information
lukechampine authored Sep 10, 2024
2 parents b2e88f1 + aa9bcae commit 8982ce1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
20 changes: 11 additions & 9 deletions analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ var Analyzer = &analysis.Analyzer{
},
}

var checkTypes bool
var clientPrefix string
var serverPrefix string

func init() {
Analyzer.Flags.BoolVar(&checkTypes, "types", true, "check that request/response types match in client and server")
Analyzer.Flags.StringVar(&clientPrefix, "cprefix", "", "client endpoint URL prefix to trim")
Analyzer.Flags.StringVar(&serverPrefix, "sprefix", "", "server endpoint URL prefix to trim")
}
Expand Down Expand Up @@ -251,7 +253,7 @@ func parseServerRoute(kv *ast.KeyValueExpr, pass *analysis.Pass) (*serverRoute,
})
return false
}
if r.request != nil && !types.Identical(typ, r.request) {
if checkTypes && r.request != nil && !types.Identical(typ, r.request) {
pass.Report(analysis.Diagnostic{
Pos: call.Args[0].Pos(),
Message: fmt.Sprintf("Decode called on %v, but was previously called on %v", typ, r.request),
Expand All @@ -269,7 +271,7 @@ func parseServerRoute(kv *ast.KeyValueExpr, pass *analysis.Pass) (*serverRoute,
return false
}
typ := typeof(call.Args[0])
if r.response != nil && !types.Identical(typ, r.response) {
if checkTypes && r.response != nil && !types.Identical(typ, r.response) {
pass.Report(analysis.Diagnostic{
Pos: call.Args[0].Pos(),
Message: fmt.Sprintf("Encode called on %v, but was previously called on %v", typ, r.response),
Expand All @@ -281,7 +283,7 @@ func parseServerRoute(kv *ast.KeyValueExpr, pass *analysis.Pass) (*serverRoute,
case "DecodeForm":
name := evalConstString(call.Args[0], pass.TypesInfo)
typ := typeof(call.Args[1])
if prev, ok := r.queryParams[name]; ok && !types.Identical(prev, typ) {
if prev, ok := r.queryParams[name]; ok && checkTypes && !types.Identical(prev, typ) {
pass.Report(analysis.Diagnostic{
Pos: call.Pos(),
Message: fmt.Sprintf("Form value %q decoded as %v, but was previously decoded as %v", name, typ, prev),
Expand All @@ -305,7 +307,7 @@ func parseServerRoute(kv *ast.KeyValueExpr, pass *analysis.Pass) (*serverRoute,
Message: fmt.Sprintf("DecodeParam called on param (%q) not present in route definition", name),
})
return false
} else if sp.typ != nil && !types.Identical(sp.typ, typ) {
} else if checkTypes && sp.typ != nil && !types.Identical(sp.typ, typ) {
pass.Report(analysis.Diagnostic{
Pos: call.Args[1].Pos(),
Message: fmt.Sprintf("Param %q decoded as %v, but was previously decoded as %v", name, typ, sp.typ),
Expand Down Expand Up @@ -335,7 +337,7 @@ func parseServerRoute(kv *ast.KeyValueExpr, pass *analysis.Pass) (*serverRoute,
Message: fmt.Sprintf("PathParam called on param (%q) not present in route definition", name),
})
return false
} else if sp.typ != nil && !types.Identical(sp.typ, typ) {
} else if checkTypes && sp.typ != nil && !types.Identical(sp.typ, typ) {
pass.Report(analysis.Diagnostic{
Pos: call.Pos(),
Message: fmt.Sprintf("Param %q decoded as %v, but was previously decoded as %v", name, typ, sp.typ),
Expand Down Expand Up @@ -771,7 +773,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
if cr.request != nil {
got := typeof(clientPass, cr.request)
want := elem(sr.request)
if !types.Identical(got, want) {
if checkTypes && !types.Identical(got, want) {
pass.Report(analysis.Diagnostic{
Pos: cr.request.Pos(),
Message: fmt.Sprintf("Client has wrong request type for %v (got %v, should be %v)", sr, got, want),
Expand All @@ -781,7 +783,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
if cr.response != nil {
got := typeof(clientPass, cr.response)
want := ptrTo(sr.response)
if !types.Identical(got, want) {
if checkTypes && !types.Identical(got, want) {
pass.Report(analysis.Diagnostic{
Pos: cr.response.Pos(),
Message: fmt.Sprintf("Client has wrong response type for %v (got %v, should be %v)", sr, got, want),
Expand All @@ -801,7 +803,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
sp := sr.pathParams[i]
got := typeof(clientPass, cp)
want := elem(sp.typ)
if !types.Identical(got, want) {
if checkTypes && !types.Identical(got, want) {
pass.Report(analysis.Diagnostic{
Pos: cp.Pos(),
Message: fmt.Sprintf("Client has wrong type for path parameter %q (got %v, should be %v)", sp.name, got, want),
Expand All @@ -819,7 +821,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
}
got := typeof(clientPass, arg)
want := elem(sq)
if !types.Identical(got, want) {
if checkTypes && !types.Identical(got, want) {
pass.Report(analysis.Diagnostic{
Pos: arg.Pos(),
Message: fmt.Sprintf("Client has wrong type for query parameter %q (got %v, should be %v)", name, got, want),
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module go.sia.tech/jape

go 1.21.7
go 1.23.0

require (
github.com/julienschmidt/httprouter v1.3.0
golang.org/x/tools v0.5.0
golang.org/x/tools v0.24.1-0.20240904200752-4fb36d15ccac
)

require (
golang.org/x/mod v0.7.0 // indirect
golang.org/x/sys v0.4.0 // indirect
golang.org/x/mod v0.20.0 // indirect
golang.org/x/sync v0.8.0 // indirect
)
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
golang.org/x/mod v0.7.0 h1:LapD9S96VoQRhi/GrNTqeBJFrUjs5UHCAtTlgwA5oZA=
golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/tools v0.5.0 h1:+bSpV5HIeWkuvgaMfI3UmKRThoTA5ODJTUd8T17NO+4=
golang.org/x/tools v0.5.0/go.mod h1:N+Kgy78s5I24c24dU8OfWNEotWjutIs8SnJvn5IDq+k=
golang.org/x/mod v0.20.0 h1:utOm6MM3R3dnawAiJgn0y+xvuYRsm1RKM/4giyfDgV0=
golang.org/x/mod v0.20.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/tools v0.24.1-0.20240904200752-4fb36d15ccac h1:kWseWh0EEXgFFSOqQPJOEHrcUGl0H5M0nWUFKZwYHIk=
golang.org/x/tools v0.24.1-0.20240904200752-4fb36d15ccac/go.mod h1:F8QO9UBew2XTeKQ8JJPGu8KTvFUe9/eXY8LDuFkePG0=

0 comments on commit 8982ce1

Please sign in to comment.