Skip to content

Commit 10325e3

Browse files
committed
Major Version update to reflect Symbol packing and unpacking
1 parent 25ff2d9 commit 10325e3

3 files changed

Lines changed: 42 additions & 14 deletions

File tree

README.md

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ mruby-simplemsgpack
44
Breaking changes
55
================
66

7-
Starting with Release 2.0 only mruby-3 is supported, if you are on an older version check out a commit from before 2021.
7+
Starting with Version 4, Symbol packing changed, see Symbol Handling (Updated) down below.
88

99
Installation
1010
============
@@ -120,24 +120,39 @@ lazy.at_pointer("/0/name/foo")
120120
# => TypeError (cannot navigate into a string)
121121
```
122122

123-
Extension Types
124-
---------------
123+
Symbol Handling (Updated)
124+
-------------------------
125+
MessagePack for mruby now provides a **built‑in, user‑configurable symbol packing strategy**.
126+
This replaces the old pattern where users had to register extension types manually for symbols.
125127

126-
To customize how objects are packed, define an [extension type](https://github.com/msgpack/msgpack/blob/master/spec.md#types-extension-type).
128+
You can choose between three strategies:
129+
130+
- **`:raw`** (default)
131+
Symbols are packed as normal MessagePack strings.
132+
133+
- **`:string`**
134+
Symbols are packed as an extension type containing their string name.
135+
136+
- **`:int`**
137+
Symbols are packed as an extension type containing their internal symbol ID.
127138

128-
By default, MessagePack packs symbols as strings and does not convert them
129-
back when unpacking them. Symbols can be preserved by registering an extension
130-
type for them:
139+
Configure the strategy globally:
131140

132141
```ruby
133-
sym_ext_type = 0
134-
MessagePack.register_pack_type(sym_ext_type, Symbol) { |symbol| symbol.to_s }
135-
MessagePack.register_unpack_type(sym_ext_type) { |data| data.to_sym }
142+
# Pack symbols as raw strings (default)
143+
MessagePack.sym_strategy(:raw)
136144

137-
MessagePack.unpack(:symbol.to_msgpack) # => :symbol
145+
# Pack symbols as ext with their string name
146+
MessagePack.sym_strategy(:string, 1)
147+
148+
# Pack symbols as ext with their integer ID
149+
MessagePack.sym_strategy(:int, 1)
138150
```
139151

140-
Other objects like classes can also be preserved:
152+
Extension Types
153+
---------------
154+
155+
To customize how objects are packed, define an [extension type](https://github.com/msgpack/msgpack/blob/master/spec.md#types-extension-type).
141156

142157
```ruby
143158
cls_ext_type = 1

mrblib/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module MessagePack
2-
VERSION="3.1"
2+
VERSION="4.0"
33
end

src/mrb_msgpack.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,9 +502,10 @@ mrb_msgpack_register_pack_type(mrb_state* mrb, mrb_value self)
502502
mrb_raise(mrb, E_TYPE_ERROR, "not a block");
503503
}
504504

505+
mrb_msgpack_ctx* ctx = MRB_MSGPACK_CONTEXT(mrb);
505506
if (mrb_class_ptr(mrb_class) == mrb->symbol_class) {
506507
mrb_raise(mrb, E_ARGUMENT_ERROR,
507-
"cannot register ext packer for Symbols");
508+
"cannot register ext packer for Symbols, use the new MessagePack.sym_strategy function.");
508509
}
509510

510511
ext_packers = mrb_const_get(mrb, self, MRB_SYM(_ExtPackers));
@@ -517,6 +518,7 @@ mrb_msgpack_register_pack_type(mrb_state* mrb, mrb_value self)
517518
return mrb_nil_value();
518519
}
519520

521+
520522
static mrb_value
521523
mrb_msgpack_ext_packer_registered(mrb_state *mrb, mrb_value self)
522524
{
@@ -930,6 +932,16 @@ mrb_msgpack_register_unpack_type(mrb_state* mrb, mrb_value self)
930932
mrb_raise(mrb, E_TYPE_ERROR, "not a block");
931933
}
932934

935+
mrb_msgpack_ctx* ctx = MRB_MSGPACK_CONTEXT(mrb);
936+
937+
// If the user is using an ext-based symbol strategy,
938+
// forbid overriding the symbol ext type.
939+
if (ctx->sym_unpacker != nullptr && type == ctx->ext_type) {
940+
mrb_raise(mrb, E_ARGUMENT_ERROR,
941+
"cannot register ext unpacker for Symbols, use MessagePack.sym_strategy instead.");
942+
}
943+
944+
// Otherwise: safe to register
933945
mrb_hash_set(mrb,
934946
mrb_const_get(mrb, self, MRB_SYM(_ExtUnpackers)),
935947
mrb_int_value(mrb, type),
@@ -938,6 +950,7 @@ mrb_msgpack_register_unpack_type(mrb_state* mrb, mrb_value self)
938950
return mrb_nil_value();
939951
}
940952

953+
941954
static mrb_value
942955
mrb_msgpack_ext_unpacker_registered(mrb_state *mrb, mrb_value self)
943956
{

0 commit comments

Comments
 (0)