MCPcopy
hub / github.com/go-gorp/gorp / readStructColumns

Method readStructColumns

db.go:281–414  ·  view source on GitHub ↗
(t reflect.Type)

Source from the content-addressed store, hash-verified

279}
280
281func (m *DbMap) readStructColumns(t reflect.Type) (cols []*ColumnMap, primaryKey []*ColumnMap) {
282 primaryKey = make([]*ColumnMap, 0)
283 n := t.NumField()
284 for i := 0; i < n; i++ {
285 f := t.Field(i)
286 if f.Anonymous && f.Type.Kind() == reflect.Struct {
287 // Recursively add nested fields in embedded structs.
288 subcols, subpk := m.readStructColumns(f.Type)
289 // Don't append nested fields that have the same field
290 // name as an already-mapped field.
291 for _, subcol := range subcols {
292 shouldAppend := true
293 for _, col := range cols {
294 if !subcol.Transient && subcol.fieldName == col.fieldName {
295 shouldAppend = false
296 break
297 }
298 }
299 if shouldAppend {
300 cols = append(cols, subcol)
301 }
302 }
303 if subpk != nil {
304 primaryKey = append(primaryKey, subpk...)
305 }
306 } else {
307 // Tag = Name { ',' Option }
308 // Option = OptionKey [ ':' OptionValue ]
309 cArguments := strings.Split(f.Tag.Get("db"), ",")
310 columnName := cArguments[0]
311 var maxSize int
312 var defaultValue string
313 var isAuto bool
314 var isPK bool
315 var isNotNull bool
316 for _, argString := range cArguments[1:] {
317 argString = strings.TrimSpace(argString)
318 arg := strings.SplitN(argString, ":", 2)
319
320 // check mandatory/unexpected option values
321 switch arg[0] {
322 case "size", "default":
323 // options requiring value
324 if len(arg) == 1 {
325 panic(fmt.Sprintf("missing option value for option %v on field %v", arg[0], f.Name))
326 }
327 default:
328 // options where value is invalid (currently all other options)
329 if len(arg) == 2 {
330 panic(fmt.Sprintf("unexpected option value for option %v on field %v", arg[0], f.Name))
331 }
332 }
333
334 switch arg[0] {
335 case "size":
336 maxSize, _ = strconv.Atoi(arg[1])
337 case "default":
338 defaultValue = arg[1]

Callers 2

AddTableDynamicMethod · 0.95

Calls 5

GetMethod · 0.65
FromDbMethod · 0.65
SqlTypeMethod · 0.65
PrintfMethod · 0.65
ValueMethod · 0.45

Tested by

no test coverage detected