|
| 1 | +package requestmigrations |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "strings" |
| 6 | +) |
| 7 | + |
| 8 | +// IsStringEmpty checks if the given string s is empty or not |
| 9 | +func isStringEmpty(s string) bool { return len(strings.TrimSpace(s)) == 0 } |
| 10 | + |
| 11 | +// dereferenceToLastPtr dereferences nested pointers down to the last pointer level. |
| 12 | +// For example: ***T -> *T, **T -> *T, *T -> *T, T -> T |
| 13 | +func dereferenceToLastPtr(t reflect.Type) reflect.Type { |
| 14 | + if t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Ptr { |
| 15 | + return dereferenceToLastPtr(t.Elem()) |
| 16 | + } |
| 17 | + return t |
| 18 | +} |
| 19 | + |
| 20 | +// typeHasInterfaceFields checks if a type has any interface fields (direct or nested). |
| 21 | +// Types with interface fields need runtime value inspection and cannot use cached graphs directly. |
| 22 | +func typeHasInterfaceFields(t reflect.Type) bool { |
| 23 | + return typeHasInterfaceFieldsRecursive(t, make(map[reflect.Type]bool)) |
| 24 | +} |
| 25 | + |
| 26 | +func typeHasInterfaceFieldsRecursive(t reflect.Type, visited map[reflect.Type]bool) bool { |
| 27 | + // Dereference pointers |
| 28 | + for t.Kind() == reflect.Ptr { |
| 29 | + t = t.Elem() |
| 30 | + } |
| 31 | + |
| 32 | + // Prevent infinite recursion for cyclic types |
| 33 | + if visited[t] { |
| 34 | + return false |
| 35 | + } |
| 36 | + visited[t] = true |
| 37 | + |
| 38 | + switch t.Kind() { |
| 39 | + case reflect.Interface: |
| 40 | + return true |
| 41 | + case reflect.Struct: |
| 42 | + for i := 0; i < t.NumField(); i++ { |
| 43 | + if typeHasInterfaceFieldsRecursive(t.Field(i).Type, visited) { |
| 44 | + return true |
| 45 | + } |
| 46 | + } |
| 47 | + case reflect.Slice, reflect.Array: |
| 48 | + return typeHasInterfaceFieldsRecursive(t.Elem(), visited) |
| 49 | + case reflect.Map: |
| 50 | + return typeHasInterfaceFieldsRecursive(t.Key(), visited) || |
| 51 | + typeHasInterfaceFieldsRecursive(t.Elem(), visited) |
| 52 | + } |
| 53 | + |
| 54 | + return false |
| 55 | +} |
0 commit comments