Skip to content

Commit ab62690

Browse files
authored
encoding.binary: fix serialize skip struct shared fields (related to issue #25600) (#25613)
1 parent 6746e5b commit ab62690

File tree

2 files changed

+55
-21
lines changed

2 files changed

+55
-21
lines changed

vlib/encoding/binary/serialize.v

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub mut:
1919

2020
// encode_binary encode a T type data into u8 array.
2121
// for encoding struct, you can use `@[serialize: '-']` to skip field.
22+
// Note: `shared` fields in struct will be skipped.
2223
pub fn encode_binary[T](obj T, config EncodeConfig) ![]u8 {
2324
mut s := EncodeState{
2425
b: []u8{cap: config.buffer_len}
@@ -56,17 +57,20 @@ fn encode_struct[T](mut s EncodeState, obj T) ! {
5657
}
5758
}
5859
if !is_skip {
59-
value := obj.$(field.name)
60-
$if field.typ is $array {
61-
encode_array(mut s, value)!
62-
} $else $if field.typ is $string {
63-
encode_string(mut s, value)!
64-
} $else $if field.typ is $struct {
65-
encode_struct(mut s, value)!
66-
} $else $if field.typ is $map {
67-
encode_map(mut s, value)!
68-
} $else {
69-
encode_primitive(mut s, value)!
60+
// TODO: support shared field in struct, currently skip.
61+
$if field.typ !is $shared {
62+
value := obj.$(field.name)
63+
$if field.typ is $array {
64+
encode_array(mut s, value)!
65+
} $else $if field.typ is $string {
66+
encode_string(mut s, value)!
67+
} $else $if field.typ is $struct {
68+
encode_struct(mut s, value)!
69+
} $else $if field.typ is $map {
70+
encode_map(mut s, value)!
71+
} $else {
72+
encode_primitive(mut s, value)!
73+
}
7074
}
7175
}
7276
}
@@ -201,6 +205,7 @@ pub mut:
201205

202206
// decode_binary decode a u8 array into T type data.
203207
// for decoding struct, you can use `@[serialize: '-']` to skip field.
208+
// Note: `shared` fields in struct will be skipped.
204209
pub fn decode_binary[T](b []u8, config DecodeConfig) !T {
205210
mut s := DecodeState{
206211
b: b
@@ -239,16 +244,19 @@ fn decode_struct[T](mut s DecodeState, _ T) !T {
239244
}
240245
}
241246
if !is_skip {
242-
$if field.typ is $array {
243-
obj.$(field.name) = decode_array(mut s, obj.$(field.name))!
244-
} $else $if field.typ is $string {
245-
obj.$(field.name) = decode_string(mut s)!
246-
} $else $if field.typ is $struct {
247-
obj.$(field.name) = decode_struct(mut s, obj.$(field.name))!
248-
} $else $if field.typ is $map {
249-
obj.$(field.name) = decode_map(mut s, obj.$(field.name))!
250-
} $else {
251-
obj.$(field.name) = decode_primitive(mut s, obj.$(field.name))!
247+
// TODO: support shared field in struct, currently skip.
248+
$if field.typ !is $shared {
249+
$if field.typ is $array {
250+
obj.$(field.name) = decode_array(mut s, obj.$(field.name))!
251+
} $else $if field.typ is $string {
252+
obj.$(field.name) = decode_string(mut s)!
253+
} $else $if field.typ is $struct {
254+
obj.$(field.name) = decode_struct(mut s, obj.$(field.name))!
255+
} $else $if field.typ is $map {
256+
obj.$(field.name) = decode_map(mut s, obj.$(field.name))!
257+
} $else {
258+
obj.$(field.name) = decode_primitive(mut s, obj.$(field.name))!
259+
}
252260
}
253261
}
254262
}

vlib/encoding/binary/serialize_test.v

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,3 +531,29 @@ fn test_encode_decode_complex() {
531531
assert b_complex != b_complex_big_endian
532532
assert a_complex == c_complex_big_endian
533533
}
534+
535+
struct SharedFieldStruct {
536+
pub mut:
537+
a int
538+
b shared map[string]string
539+
c int
540+
}
541+
542+
fn test_skip_shared_field() {
543+
x := SharedFieldStruct{
544+
a: 100
545+
b: {
546+
'a': 'a'
547+
'b': 'b'
548+
}
549+
c: 200
550+
}
551+
x_u8 := encode_binary(x)!
552+
assert x_u8 == [u8(100), 0, 0, 0, 0, 0, 0, 0, 200, 0, 0, 0, 0, 0, 0, 0]
553+
y := decode_binary[SharedFieldStruct](x_u8)!
554+
assert '${y}' == 'binary.SharedFieldStruct{
555+
a: 100
556+
b: {}
557+
c: 200
558+
}'
559+
}

0 commit comments

Comments
 (0)