@@ -78,6 +78,7 @@ type column struct {
78
78
Name string
79
79
Type string
80
80
Constraint string
81
+ ID bool
81
82
}
82
83
83
84
type outputMode uint8
@@ -104,6 +105,22 @@ type scaffoldCommandArgs struct {
104
105
Output outputMode
105
106
}
106
107
108
+ func parseColumnDefinition (s string ) (column , error ) {
109
+ parts := strings .Split (s , ":" )
110
+ if len (parts ) < 2 || len (parts ) > 3 {
111
+ return column {}, fmt .Errorf ("%w: invalid <column>: '%s', expected '<name>:<type>' or '<name>:<type>:<constraint>'" , errBadArgument , s )
112
+ }
113
+ col := column {
114
+ ID : strings .ToLower (parts [0 ]) == * idColumnFlag ,
115
+ Name : parts [0 ],
116
+ Type : parts [1 ],
117
+ }
118
+ if len (parts ) == 3 {
119
+ col .Constraint = parts [2 ]
120
+ }
121
+ return col , nil
122
+ }
123
+
107
124
func parseScaffoldCommandArgs (args []string ) (* scaffoldCommandArgs , error ) {
108
125
if len (args ) == 0 {
109
126
return nil , fmt .Errorf ("%w: missing <name> and <column>" , errBadArgument )
@@ -134,26 +151,18 @@ func parseScaffoldCommandArgs(args []string) (*scaffoldCommandArgs, error) {
134
151
}
135
152
136
153
for _ , arg := range args [1 :] {
137
- parts := strings .Split (arg , ":" )
138
- if len (parts ) < 2 || len (parts ) > 3 {
139
- return nil , fmt .Errorf ("%w: invalid <column>: '%s', expected '<name>:<type>' or '<name>:<type>:<constraint>'" , errBadArgument , arg )
140
- }
141
- col := column {
142
- Name : parts [0 ],
143
- Type : parts [1 ],
154
+ col , err := parseColumnDefinition (arg )
155
+ if err != nil {
156
+ return nil , err
144
157
}
145
158
if len (col .Name ) > sca .LongestName {
146
159
sca .LongestName = len (col .Name )
147
160
}
148
161
if len (col .Type ) > sca .LongestType {
149
162
sca .LongestType = len (col .Type )
150
163
}
151
- if len (parts ) == 3 {
152
- col .Constraint = parts [2 ]
153
- }
154
-
155
164
sca .Columns = append (sca .Columns , col )
156
- if strings . ToLower ( parts [ 0 ]) == * idColumnFlag {
165
+ if col . ID {
157
166
sca .IDColumn = & col
158
167
} else {
159
168
sca .NonIDColumns = append (sca .NonIDColumns , col )
0 commit comments