MCPcopy
hub / github.com/sqlc-dev/sqlc / columnsToStruct

Function columnsToStruct

internal/codegen/golang/result.go:370–454  ·  view source on GitHub ↗

It's possible that this method will generate duplicate JSON tag values Columns: count, count, count_2 Fields: Count, Count_2, Count2 JSON tags: count, count_2, count_2 This is unlikely to happen, so don't fix it yet

(req *plugin.GenerateRequest, options *opts.Options, name string, columns []goColumn, useID bool)

Source from the content-addressed store, hash-verified

368//
369// This is unlikely to happen, so don't fix it yet
370func columnsToStruct(req *plugin.GenerateRequest, options *opts.Options, name string, columns []goColumn, useID bool) (*Struct, error) {
371 gs := Struct{
372 Name: name,
373 }
374 seen := map[string][]int{}
375 suffixes := map[int]int{}
376 for i, c := range columns {
377 colName := columnName(c.Column, i)
378 tagName := colName
379
380 // override col/tag with expected model name
381 if c.embed != nil {
382 colName = c.embed.modelName
383 tagName = SetCaseStyle(colName, "snake")
384 }
385
386 fieldName := StructName(colName, options)
387 baseFieldName := fieldName
388 // Track suffixes by the ID of the column, so that columns referring to the same numbered parameter can be
389 // reused.
390 suffix := 0
391 if o, ok := suffixes[c.id]; ok && useID {
392 suffix = o
393 } else if v := len(seen[fieldName]); v > 0 && !c.IsNamedParam {
394 suffix = v + 1
395 }
396 suffixes[c.id] = suffix
397 if suffix > 0 {
398 tagName = fmt.Sprintf("%s_%d", tagName, suffix)
399 fieldName = fmt.Sprintf("%s_%d", fieldName, suffix)
400 }
401 tags := map[string]string{}
402 if options.EmitDbTags {
403 tags["db"] = tagName
404 }
405 if options.EmitJsonTags {
406 tags["json"] = JSONTagName(tagName, options)
407 }
408 addExtraGoStructTags(tags, req, options, c.Column)
409 f := Field{
410 Name: fieldName,
411 DBName: colName,
412 Tags: tags,
413 Column: c.Column,
414 }
415 if c.embed == nil {
416 f.Type = goType(req, options, c.Column)
417 } else {
418 f.Type = c.embed.modelType
419 f.EmbedFields = c.embed.fields
420 }
421
422 gs.Fields = append(gs.Fields, f)
423 if _, found := seen[baseFieldName]; !found {
424 seen[baseFieldName] = []int{i}
425 } else {
426 seen[baseFieldName] = append(seen[baseFieldName], i)
427 }

Callers 1

buildQueriesFunction · 0.85

Calls 7

columnNameFunction · 0.85
SetCaseStyleFunction · 0.85
StructNameFunction · 0.85
JSONTagNameFunction · 0.85
addExtraGoStructTagsFunction · 0.85
goTypeFunction · 0.85

Tested by

no test coverage detected