From ab626909afbab80067d7d7550400ac75ec29a316 Mon Sep 17 00:00:00 2001 From: kbkpbot Date: Wed, 29 Oct 2025 17:58:05 +0800 Subject: [PATCH] encoding.binary: fix serialize skip struct shared fields (related to issue #25600) (#25613) --- vlib/encoding/binary/serialize.v | 50 ++++++++++++++++----------- vlib/encoding/binary/serialize_test.v | 26 ++++++++++++++ 2 files changed, 55 insertions(+), 21 deletions(-) diff --git a/vlib/encoding/binary/serialize.v b/vlib/encoding/binary/serialize.v index 328da6ff3..89088959b 100644 --- a/vlib/encoding/binary/serialize.v +++ b/vlib/encoding/binary/serialize.v @@ -19,6 +19,7 @@ pub mut: // encode_binary encode a T type data into u8 array. // for encoding struct, you can use `@[serialize: '-']` to skip field. +// Note: `shared` fields in struct will be skipped. pub fn encode_binary[T](obj T, config EncodeConfig) ![]u8 { mut s := EncodeState{ b: []u8{cap: config.buffer_len} @@ -56,17 +57,20 @@ fn encode_struct[T](mut s EncodeState, obj T) ! { } } if !is_skip { - value := obj.$(field.name) - $if field.typ is $array { - encode_array(mut s, value)! - } $else $if field.typ is $string { - encode_string(mut s, value)! - } $else $if field.typ is $struct { - encode_struct(mut s, value)! - } $else $if field.typ is $map { - encode_map(mut s, value)! - } $else { - encode_primitive(mut s, value)! + // TODO: support shared field in struct, currently skip. + $if field.typ !is $shared { + value := obj.$(field.name) + $if field.typ is $array { + encode_array(mut s, value)! + } $else $if field.typ is $string { + encode_string(mut s, value)! + } $else $if field.typ is $struct { + encode_struct(mut s, value)! + } $else $if field.typ is $map { + encode_map(mut s, value)! + } $else { + encode_primitive(mut s, value)! + } } } } @@ -201,6 +205,7 @@ pub mut: // decode_binary decode a u8 array into T type data. // for decoding struct, you can use `@[serialize: '-']` to skip field. +// Note: `shared` fields in struct will be skipped. pub fn decode_binary[T](b []u8, config DecodeConfig) !T { mut s := DecodeState{ b: b @@ -239,16 +244,19 @@ fn decode_struct[T](mut s DecodeState, _ T) !T { } } if !is_skip { - $if field.typ is $array { - obj.$(field.name) = decode_array(mut s, obj.$(field.name))! - } $else $if field.typ is $string { - obj.$(field.name) = decode_string(mut s)! - } $else $if field.typ is $struct { - obj.$(field.name) = decode_struct(mut s, obj.$(field.name))! - } $else $if field.typ is $map { - obj.$(field.name) = decode_map(mut s, obj.$(field.name))! - } $else { - obj.$(field.name) = decode_primitive(mut s, obj.$(field.name))! + // TODO: support shared field in struct, currently skip. + $if field.typ !is $shared { + $if field.typ is $array { + obj.$(field.name) = decode_array(mut s, obj.$(field.name))! + } $else $if field.typ is $string { + obj.$(field.name) = decode_string(mut s)! + } $else $if field.typ is $struct { + obj.$(field.name) = decode_struct(mut s, obj.$(field.name))! + } $else $if field.typ is $map { + obj.$(field.name) = decode_map(mut s, obj.$(field.name))! + } $else { + obj.$(field.name) = decode_primitive(mut s, obj.$(field.name))! + } } } } diff --git a/vlib/encoding/binary/serialize_test.v b/vlib/encoding/binary/serialize_test.v index 4fb3111c7..da936e1ce 100644 --- a/vlib/encoding/binary/serialize_test.v +++ b/vlib/encoding/binary/serialize_test.v @@ -531,3 +531,29 @@ fn test_encode_decode_complex() { assert b_complex != b_complex_big_endian assert a_complex == c_complex_big_endian } + +struct SharedFieldStruct { +pub mut: + a int + b shared map[string]string + c int +} + +fn test_skip_shared_field() { + x := SharedFieldStruct{ + a: 100 + b: { + 'a': 'a' + 'b': 'b' + } + c: 200 + } + x_u8 := encode_binary(x)! + assert x_u8 == [u8(100), 0, 0, 0, 0, 0, 0, 0, 200, 0, 0, 0, 0, 0, 0, 0] + y := decode_binary[SharedFieldStruct](x_u8)! + assert '${y}' == 'binary.SharedFieldStruct{ + a: 100 + b: {} + c: 200 +}' +} -- 2.39.5