@@ -255,3 +255,66 @@ func GetArgType(argType reflect.Type, name string) (reflect.Type, error) {
255255
256256 return recursiveFunc (argType , strings .Split (name , "." ))
257257}
258+
259+ var listArgTypeFieldsSkippedArguments = []string {
260+ "page" ,
261+ "page-size" ,
262+ "per-page" ,
263+ }
264+
265+ func listArgTypeFields (base string , argType reflect.Type ) []string {
266+ if argType .Kind () != reflect .Ptr {
267+ // Can be a handled type like time.Time
268+ // If so, use it like a scalar type
269+ _ , isHandled := unmarshalFuncs [argType ]
270+ if isHandled {
271+ return []string {base }
272+ }
273+ }
274+
275+ switch argType .Kind () {
276+ case reflect .Ptr :
277+ return listArgTypeFields (base , argType .Elem ())
278+
279+ case reflect .Slice :
280+ return listArgTypeFields (base + "." + sliceSchema , argType .Elem ())
281+
282+ case reflect .Map :
283+ return listArgTypeFields (base + "." + mapSchema , argType .Elem ())
284+
285+ case reflect .Struct :
286+ fields := []string (nil )
287+
288+ for i := 0 ; i < argType .NumField (); i ++ {
289+ field := argType .Field (i )
290+ fieldBase := base
291+
292+ // If this is an embedded struct, skip adding its name to base
293+ if field .Anonymous {
294+ fields = append (fields , listArgTypeFields (fieldBase , field .Type )... )
295+ continue
296+ }
297+
298+ if fieldBase == "" {
299+ fieldBase = strcase .ToBashArg (field .Name )
300+ } else {
301+ fieldBase += "." + strcase .ToBashArg (field .Name )
302+ }
303+ fields = append (fields , listArgTypeFields (fieldBase , field .Type )... )
304+ }
305+
306+ return fields
307+ default :
308+ for _ , skippedArg := range listArgTypeFieldsSkippedArguments {
309+ if base == skippedArg {
310+ return []string {}
311+ }
312+ }
313+ return []string {base }
314+ }
315+ }
316+
317+ // ListArgTypeFields take a go struct and return a list of name that comply with ArgSpec name notation (e.g "friends.{index}.name")
318+ func ListArgTypeFields (argType reflect.Type ) []string {
319+ return listArgTypeFields ("" , argType )
320+ }
0 commit comments