Skip to content

Commit 3aad309

Browse files
enhance struct value construction
1 parent cfa64cd commit 3aad309

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

internal/value/value.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,19 @@ type StructValueProto struct {
502502
Values []*Ydb.Value
503503
}
504504

505+
func (s *StructValueProto) Grow(size int) {
506+
if cap(s.Fields) < size {
507+
newFields := make([]StructField, 0, size)
508+
newFields = append(newFields, s.Fields...)
509+
s.Fields = newFields
510+
}
511+
if cap(s.Values) < size {
512+
newValues := make([]*Ydb.Value, 0, size)
513+
newValues = append(newValues, s.Values...)
514+
s.Values = newValues
515+
}
516+
}
517+
505518
func (s *StructValueProto) Add(name string, value V) {
506519
s.Fields = append(s.Fields, StructField{
507520
Name: name,

table/types/value.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,18 +147,28 @@ func ListValue(vs ...Value) Value {
147147

148148
type tStructValueProto value.StructValueProto
149149

150-
type StructValueOption func(*tStructValueProto)
150+
type StructValueOption interface {
151+
apply(*tStructValueProto)
152+
}
153+
154+
type structField struct {
155+
name string
156+
value Value
157+
}
158+
159+
func (f structField) apply(p *tStructValueProto) {
160+
(*value.StructValueProto)(p).Add(f.name, f.value)
161+
}
151162

152163
func StructFieldValue(name string, v Value) StructValueOption {
153-
return func(p *tStructValueProto) {
154-
(*value.StructValueProto)(p).Add(name, v)
155-
}
164+
return structField{name: name, value: v}
156165
}
157166

158167
func StructValue(opts ...StructValueOption) Value {
159168
var p tStructValueProto
169+
(*value.StructValueProto)(&p).Grow(len(opts))
160170
for _, opt := range opts {
161-
opt(&p)
171+
opt.apply(&p)
162172
}
163173
return value.StructValue((*value.StructValueProto)(&p))
164174
}

0 commit comments

Comments
 (0)