MCPcopy Create free account
hub / github.com/uptrace/bun / arrayAppender

Method arrayAppender

dialect/pgdialect/array.go:75–139  ·  view source on GitHub ↗

------------------------------------------------------------------------------

(typ reflect.Type)

Source from the content-addressed store, hash-verified

73//------------------------------------------------------------------------------
74
75func (d *Dialect) arrayAppender(typ reflect.Type) schema.AppenderFunc {
76 kind := typ.Kind()
77
78 switch kind {
79 case reflect.Ptr:
80 if fn := d.arrayAppender(typ.Elem()); fn != nil {
81 return schema.PtrAppender(fn)
82 }
83 case reflect.Slice, reflect.Array:
84 // continue below
85 default:
86 return nil
87 }
88
89 elemType := typ.Elem()
90
91 if kind == reflect.Slice {
92 switch elemType {
93 case stringType:
94 return appendStringSliceValue
95 case intType:
96 return appendIntSliceValue
97 case int64Type:
98 return appendInt64SliceValue
99 case float64Type:
100 return appendFloat64SliceValue
101 case timeType:
102 return appendTimeSliceValue
103 }
104 }
105
106 appendElem := d.arrayElemAppender(elemType)
107 if appendElem == nil {
108 panic(fmt.Errorf("pgdialect: %s is not supported", typ))
109 }
110
111 return func(gen schema.QueryGen, b []byte, v reflect.Value) []byte {
112 kind := v.Kind()
113 switch kind {
114 case reflect.Ptr, reflect.Slice:
115 if v.IsNil() {
116 return dialect.AppendNull(b)
117 }
118 }
119
120 if kind == reflect.Ptr {
121 v = v.Elem()
122 }
123
124 b = append(b, "'{"...)
125
126 ln := v.Len()
127 for i := 0; i < ln; i++ {
128 elem := v.Index(i)
129 if i > 0 {
130 b = append(b, ',')
131 }
132 b = appendElem(gen, b, elem)

Callers 2

onFieldMethod · 0.95
ArrayFunction · 0.80

Calls 7

arrayElemAppenderMethod · 0.95
PtrAppenderFunction · 0.92
AppendNullFunction · 0.92
appendElemFunction · 0.85
ElemMethod · 0.80
LenMethod · 0.45
IndexMethod · 0.45

Tested by

no test coverage detected