github.com/niko0xdev/gqlgen@v0.17.55-0.20240120102243-2ecff98c3e37/codegen/util.go (about) 1 package codegen 2 3 import ( 4 "fmt" 5 "go/types" 6 "strings" 7 ) 8 9 func findGoNamedType(def types.Type) (*types.Named, error) { 10 if def == nil { 11 return nil, nil 12 } 13 14 namedType, ok := def.(*types.Named) 15 if !ok { 16 return nil, fmt.Errorf("expected %s to be a named type, instead found %T\n", def.String(), def) 17 } 18 19 return namedType, nil 20 } 21 22 func findGoInterface(def types.Type) (*types.Interface, error) { 23 if def == nil { 24 return nil, nil 25 } 26 namedType, err := findGoNamedType(def) 27 if err != nil { 28 return nil, err 29 } 30 if namedType == nil { 31 return nil, nil 32 } 33 34 underlying, ok := namedType.Underlying().(*types.Interface) 35 if !ok { 36 return nil, fmt.Errorf("expected %s to be a named interface, instead found %s", def.String(), namedType.String()) 37 } 38 39 return underlying, nil 40 } 41 42 func equalFieldName(source, target string) bool { 43 source = strings.ReplaceAll(source, "_", "") 44 source = strings.ReplaceAll(source, ",omitempty", "") 45 target = strings.ReplaceAll(target, "_", "") 46 return strings.EqualFold(source, target) 47 }