...

Source file src/github.com/99designs/gqlgen/plugin/modelgen/types.go

Documentation: github.com/99designs/gqlgen/plugin/modelgen

     1  package modelgen
     2  
     3  import (
     4  	"go/types"
     5  	"strings"
     6  )
     7  
     8  // buildType constructs a types.Type for the given string (using the syntax
     9  // from the extra field config Type field).
    10  func buildType(typeString string) types.Type {
    11  	switch {
    12  	case typeString[0] == '*':
    13  		return types.NewPointer(buildType(typeString[1:]))
    14  	case strings.HasPrefix(typeString, "[]"):
    15  		return types.NewSlice(buildType(typeString[2:]))
    16  	default:
    17  		return buildNamedType(typeString)
    18  	}
    19  }
    20  
    21  // buildNamedType returns the specified named or builtin type.
    22  //
    23  // Note that we don't look up the full types.Type object from the appropriate
    24  // package -- gqlgen doesn't give us the package-map we'd need to do so.
    25  // Instead we construct a placeholder type that has all the fields gqlgen
    26  // wants. This is roughly what gqlgen itself does, anyway:
    27  // https://github.com/99designs/gqlgen/blob/master/plugin/modelgen/models.go#L119
    28  func buildNamedType(fullName string) types.Type {
    29  	dotIndex := strings.LastIndex(fullName, ".")
    30  	if dotIndex == -1 { // builtinType
    31  		return types.Universe.Lookup(fullName).Type()
    32  	}
    33  
    34  	// type is pkg.Name
    35  	pkgPath := fullName[:dotIndex]
    36  	typeName := fullName[dotIndex+1:]
    37  
    38  	pkgName := pkgPath
    39  	slashIndex := strings.LastIndex(pkgPath, "/")
    40  	if slashIndex != -1 {
    41  		pkgName = pkgPath[slashIndex+1:]
    42  	}
    43  
    44  	pkg := types.NewPackage(pkgPath, pkgName)
    45  	// gqlgen doesn't use some of the fields, so we leave them 0/nil
    46  	return types.NewNamed(types.NewTypeName(0, pkg, typeName, nil), nil, nil)
    47  }
    48  

View as plain text