Skip to content

Commit

Permalink
Add nullable option to codec (#2171)
Browse files Browse the repository at this point in the history
Signed-off-by: Cesar <[email protected]>
Co-authored-by: Stephen Buttolph <[email protected]>
Co-authored-by: Dan Laine <[email protected]>
  • Loading branch information
3 people authored Nov 2, 2023
1 parent 4957ccb commit 36d1630
Show file tree
Hide file tree
Showing 4 changed files with 283 additions and 77 deletions.
19 changes: 17 additions & 2 deletions codec/reflectcodec/struct_fielder.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@ const (

// TagValue is the value the tag must have to be serialized.
TagValue = "true"

// TagValue is the value the tag must have to be serialized, this variant
// includes the nullable option
TagWithNullableValue = "true,nullable"
)

var _ StructFielder = (*structFielder)(nil)

type FieldDesc struct {
Index int
MaxSliceLen uint32
Nullable bool
}

// StructFielder handles discovery of serializable fields in a struct.
Expand Down Expand Up @@ -82,10 +87,19 @@ func (s *structFielder) GetSerializedFields(t reflect.Type) ([]FieldDesc, error)
// Multiple tags per fields can be specified.
// Serialize/Deserialize field if it has
// any tag with the right value
captureField := false
var (
captureField bool
nullable bool
)
for _, tag := range s.tags {
if field.Tag.Get(tag) == TagValue {
switch field.Tag.Get(tag) {
case TagValue:
captureField = true
case TagWithNullableValue:
captureField = true
nullable = true
}
if captureField {
break
}
}
Expand All @@ -107,6 +121,7 @@ func (s *structFielder) GetSerializedFields(t reflect.Type) ([]FieldDesc, error)
serializedFields = append(serializedFields, FieldDesc{
Index: i,
MaxSliceLen: maxSliceLen,
Nullable: nullable,
})
}
s.serializedFieldIndices[t] = serializedFields // cache result
Expand Down
Loading

0 comments on commit 36d1630

Please sign in to comment.