@@ -47,62 +47,74 @@ func run(pass *analysis.Pass) (interface{}, error) {
47
47
i .Preorder (nodeFilter , func (n ast.Node ) {
48
48
call := n .(* ast.CallExpr )
49
49
target := typeutil .Callee (pass .TypesInfo , call )
50
- numArgs := 3
50
+ numArgs := len ( call . Args )
51
51
if target == nil {
52
52
return
53
53
} else if target .Pkg () == nil || ! (target .Pkg ().Path () == "ghe.anduril.dev/anduril/graphene-go/pkg/graphene" &&
54
- (target .Name () == getFn || target .Name () == subscribeFn )) || len (call .Args ) != numArgs {
54
+ ((target .Name () == getFn && (numArgs == 2 || numArgs == 3 )) ||
55
+ (target .Name () == subscribeFn ) && numArgs != 3 )) {
55
56
return
56
57
}
57
58
if selector , ok := call .Fun .(* ast.SelectorExpr ); ok {
58
59
fnName := selector .Sel .Name
59
60
isGetConfig := fnName == getFn
60
61
isSubscribeConfig := fnName == subscribeFn
61
62
if isGetConfig || isSubscribeConfig {
62
- defaultConfArg := call .Args [1 ]
63
- defaultConfArgType := pass .TypesInfo .Types [defaultConfArg ].Type
64
- defaultConfig := getStructType (defaultConfArgType )
65
- if defaultConfig == nil {
66
- pass .Reportf (
67
- defaultConfArg .Pos (),
68
- "expected 2nd arg to be non nil but got %v" ,
69
- defaultConfig ,
70
- )
71
- return
72
- }
73
- validateFieldSerialization (pass , defaultConfArg .Pos (), defaultConfig , defaultConfArgType .String ())
74
- arg3 := call .Args [2 ]
75
- if isSubscribeConfig {
76
- validateTypeConversion (arg3 , defaultConfArgType .String (), pass )
77
- } else if isGetConfig {
78
- checkPointerArg (arg3 , "" , defaultConfArgType .String (), arg3 .Pos (), pass , false )
63
+ if numArgs == 3 {
64
+ // Using Subscribe or Get from RuntimeConfig
65
+ defaultConfArg := call .Args [1 ]
66
+ defaultConfArgType := pass .TypesInfo .Types [defaultConfArg ].Type
67
+ defaultConfig := getStructType (defaultConfArgType )
68
+ if defaultConfig == nil {
69
+ pass .Reportf (
70
+ defaultConfArg .Pos (),
71
+ "expected 2nd arg to be non nil but got %v" ,
72
+ defaultConfig ,
73
+ )
74
+ return
75
+ }
76
+ validateFieldSerialization (pass , defaultConfArg .Pos (), defaultConfig , defaultConfArgType .String ())
77
+ arg3 := call .Args [2 ]
78
+ if isSubscribeConfig {
79
+ validateTypeConversion (arg3 , defaultConfArgType .String (), pass )
80
+ } else if isGetConfig {
81
+ checkPointerArg (arg3 , "" , defaultConfArgType .String (), arg3 .Pos (), pass , "3rd" , false )
82
+ }
83
+ } else {
84
+ // Using Get from RuntimeConfigV2
85
+ arg2 := call .Args [1 ]
86
+ checkPointerArg (arg2 , "" , "" , arg2 .Pos (), pass , "2nd" , false )
79
87
}
80
88
}
81
89
}
82
90
})
83
91
return nil , nil
84
92
}
85
93
86
- func checkPointerArg (arg ast.Expr , argName , defType string , pos token.Pos , pass * analysis.Pass , isRef bool ) {
94
+ func checkPointerArg (arg ast.Expr , argName , defType string , pos token.Pos , pass * analysis.Pass , numArgsString string , isRef bool ) {
87
95
switch t := arg .(type ) {
88
96
case * ast.Ident :
89
97
if t .Obj == nil {
90
- pass .Reportf (t .Pos (), "expected configuration object as 3rd arg but got nil" )
98
+ pass .Reportf (t .Pos (), "expected configuration object as %s arg but got nil" , numArgsString )
91
99
return
92
100
}
93
101
if varDec , ok := t .Obj .Decl .(* ast.AssignStmt ); ok {
94
102
if len (varDec .Rhs ) == 1 {
95
- checkPointerArg (varDec .Rhs [0 ], t .Name , defType , pos , pass , isRef )
103
+ checkPointerArg (varDec .Rhs [0 ], t .Name , defType , pos , pass , numArgsString , isRef )
96
104
}
97
105
}
98
106
case * ast.UnaryExpr :
99
107
if ! t .Op .IsOperator () || t .Op .String () != "&" {
100
- pass .Reportf (pos , "expected ref as 3rd arg to Get" )
108
+ pass .Reportf (pos , "expected ref as %s arg to Get" , numArgsString )
101
109
}
102
- checkPointerArg (t .X , argName , defType , pos , pass , true )
110
+ checkPointerArg (t .X , argName , defType , pos , pass , numArgsString , true )
103
111
case * ast.CompositeLit :
104
112
if ! isRef {
105
- pass .Reportf (pos , "expected ref as 3rd arg to Get" )
113
+ pass .Reportf (pos , "expected ref as %s arg to Get" , numArgsString )
114
+ }
115
+ if defType == "" {
116
+ // Using RuntimeConfigV2, skip SameType checks
117
+ return
106
118
}
107
119
switch sl := t .Type .(type ) {
108
120
case * ast.Ident :
@@ -112,10 +124,10 @@ func checkPointerArg(arg ast.Expr, argName, defType string, pos token.Pos, pass
112
124
}
113
125
case * ast.SelectorExpr :
114
126
if ! isRef {
115
- pass .Reportf (pos , "expected ref as 3rd arg to Get" )
127
+ pass .Reportf (pos , "expected ref as %s arg to Get" , numArgsString )
116
128
}
117
129
default :
118
- pass .Reportf (pos , "expected ref as 3rd arg to Get" )
130
+ pass .Reportf (pos , "expected ref as %s arg to Get" , numArgsString )
119
131
}
120
132
}
121
133
0 commit comments