|
4 | 4 | "encoding/json" |
5 | 5 | "errors" |
6 | 6 | "fmt" |
| 7 | + "go/types" |
7 | 8 | "io" |
8 | 9 | "path/filepath" |
9 | 10 | "strings" |
@@ -66,10 +67,11 @@ type Override struct { |
66 | 67 | // fully qualified name of the column, e.g. `accounts.id` |
67 | 68 | Column string `json:"column"` |
68 | 69 |
|
69 | | - columnName string |
70 | | - table pg.FQN |
71 | | - goTypeName string |
72 | | - goPackage string |
| 70 | + columnName string |
| 71 | + table pg.FQN |
| 72 | + goTypeName string |
| 73 | + goPackage string |
| 74 | + goBasicType bool |
73 | 75 | } |
74 | 76 |
|
75 | 77 | func (o *Override) Parse() error { |
@@ -101,26 +103,49 @@ func (o *Override) Parse() error { |
101 | 103 |
|
102 | 104 | // validate GoType |
103 | 105 | lastDot := strings.LastIndex(o.GoType, ".") |
104 | | - if lastDot == -1 { |
105 | | - return fmt.Errorf("Package override `go_type` specifier %q is not the proper format, expected 'package.type', e.g. 'github.com/segmentio/ksuid.KSUID'", o.GoType) |
106 | | - } |
107 | 106 | lastSlash := strings.LastIndex(o.GoType, "/") |
108 | | - if lastSlash == -1 { |
109 | | - return fmt.Errorf("Package override `go_type` specifier %q is not the proper format, expected 'package.type', e.g. 'github.com/segmentio/ksuid.KSUID'", o.GoType) |
110 | | - } |
111 | | - typename := o.GoType[lastSlash+1:] |
112 | | - if strings.HasPrefix(typename, "go-") { |
113 | | - // a package name beginning with "go-" will give syntax errors in |
114 | | - // generated code. We should do the right thing and get the actual |
115 | | - // import name, but in lieu of that, stripping the leading "go-" may get |
116 | | - // us what we want. |
117 | | - typename = typename[len("go-"):] |
118 | | - } |
119 | | - if strings.HasSuffix(typename, "-go") { |
120 | | - typename = typename[:len(typename)-len("-go")] |
| 107 | + typename := o.GoType |
| 108 | + if lastDot == -1 && lastSlash == -1 { |
| 109 | + // if the type name has no slash and no dot, validate that the type is a basic Go type |
| 110 | + var found bool |
| 111 | + for _, typ := range types.Typ { |
| 112 | + info := typ.Info() |
| 113 | + if info == 0 { |
| 114 | + continue |
| 115 | + } |
| 116 | + if info&types.IsUntyped != 0 { |
| 117 | + continue |
| 118 | + } |
| 119 | + if typename == typ.Name() { |
| 120 | + found = true |
| 121 | + } |
| 122 | + } |
| 123 | + if !found { |
| 124 | + return fmt.Errorf("Package override `go_type` specifier %q is not a Go basic type e.g. 'string'", o.GoType) |
| 125 | + } |
| 126 | + o.goBasicType = true |
| 127 | + } else { |
| 128 | + // assume the type lives in a Go package |
| 129 | + if lastDot == -1 { |
| 130 | + return fmt.Errorf("Package override `go_type` specifier %q is not the proper format, expected 'package.type', e.g. 'github.com/segmentio/ksuid.KSUID'", o.GoType) |
| 131 | + } |
| 132 | + if lastSlash == -1 { |
| 133 | + return fmt.Errorf("Package override `go_type` specifier %q is not the proper format, expected 'package.type', e.g. 'github.com/segmentio/ksuid.KSUID'", o.GoType) |
| 134 | + } |
| 135 | + typename = o.GoType[lastSlash+1:] |
| 136 | + if strings.HasPrefix(typename, "go-") { |
| 137 | + // a package name beginning with "go-" will give syntax errors in |
| 138 | + // generated code. We should do the right thing and get the actual |
| 139 | + // import name, but in lieu of that, stripping the leading "go-" may get |
| 140 | + // us what we want. |
| 141 | + typename = typename[len("go-"):] |
| 142 | + } |
| 143 | + if strings.HasSuffix(typename, "-go") { |
| 144 | + typename = typename[:len(typename)-len("-go")] |
| 145 | + } |
| 146 | + o.goPackage = o.GoType[:lastDot] |
121 | 147 | } |
122 | 148 | o.goTypeName = typename |
123 | | - o.goPackage = o.GoType[:lastDot] |
124 | 149 | isPointer := o.GoType[0] == '*' |
125 | 150 | if isPointer { |
126 | 151 | o.goPackage = o.goPackage[1:] |
|
0 commit comments