Skip to content

Commit 7b5023f

Browse files
committed
chore: upgraded dependencies
1 parent 55d4c47 commit 7b5023f

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

encoder/protojson/mapper.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type (
1515
// Mapper is the protoJSON mapper struct
1616
Mapper struct {
1717
reflectType reflect.Type
18+
optionalFields map[string]struct{}
1819
protoMessageFields map[string]struct{}
1920
regularFields map[string]struct{}
2021
jsonFieldNames map[string]string
@@ -43,6 +44,7 @@ func NewMapper(structInstance any) (*Mapper, error) {
4344
reflectedValue := goreflect.GetDereferencedValue(structInstance)
4445

4546
// Prepare the different maps
47+
optionalFields := make(map[string]struct{})
4648
protoMessageFields := make(map[string]struct{})
4749
regularFields := make(map[string]struct{})
4850
jsonFieldNames := make(map[string]string)
@@ -72,6 +74,11 @@ func NewMapper(structInstance any) (*Mapper, error) {
7274
if err != nil {
7375
return nil, err
7476
}
77+
78+
// Check if the field is optional
79+
if gostringsjson.IsJSONFieldOptional(jsonTag){
80+
optionalFields[fieldName] = struct{}{}
81+
}
7582

7683
// Store the JSON field name
7784
jsonFieldNames[fieldName] = jsonFieldName
@@ -101,6 +108,7 @@ func NewMapper(structInstance any) (*Mapper, error) {
101108
}
102109
return &Mapper{
103110
reflectType: reflectedType,
111+
optionalFields: optionalFields,
104112
protoMessageFields: protoMessageFields,
105113
regularFields: regularFields,
106114
jsonFieldNames: jsonFieldNames,
@@ -161,6 +169,14 @@ func (m *Mapper) PrecomputeMarshalByReflection(
161169

162170
// Get the field value
163171
fieldValueInterface := fieldValue.Interface()
172+
173+
// Check if the field is optional and is nil
174+
if _, optionalOk := m.optionalFields[fieldName]; optionalOk {
175+
if fieldValue.IsNil() {
176+
// Skip nil optional fields
177+
continue
178+
}
179+
}
164180

165181
// Check if the field is a regular field
166182
if _, regularOk := m.regularFields[fieldName]; regularOk {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ go 1.25.1
44

55
require (
66
github.com/ralvarezdev/go-reflect v0.3.1
7-
github.com/ralvarezdev/go-strings v0.2.1
7+
github.com/ralvarezdev/go-strings v0.2.2
88
google.golang.org/protobuf v1.36.10
99
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
22
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
33
github.com/ralvarezdev/go-reflect v0.3.1 h1:+u59QNddIwI0lQWhWqO5gYGKC4oR0DP50uEpLxwdS/4=
44
github.com/ralvarezdev/go-reflect v0.3.1/go.mod h1:CsZqMmJCXYow9l2YQIdvIe/q7aeRtlA3gq0r9dmLEN0=
5-
github.com/ralvarezdev/go-strings v0.2.1 h1:bWq0bzzGrPfCrdK5ptUT6+xTMuvvXQ0Q3fARFWV/7AY=
6-
github.com/ralvarezdev/go-strings v0.2.1/go.mod h1:8sFOqmPJpqzS7bTjf91EzUCITnwpmkfifwY80GxV5r8=
5+
github.com/ralvarezdev/go-strings v0.2.2 h1:lqrI4GJdA/fIDNGgNk0O0ja2YE3jG9yQpql0mA+J4Fk=
6+
github.com/ralvarezdev/go-strings v0.2.2/go.mod h1:8sFOqmPJpqzS7bTjf91EzUCITnwpmkfifwY80GxV5r8=
77
google.golang.org/protobuf v1.36.10 h1:AYd7cD/uASjIL6Q9LiTjz8JLcrh/88q5UObnmY3aOOE=
88
google.golang.org/protobuf v1.36.10/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=

0 commit comments

Comments
 (0)