You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+16-8Lines changed: 16 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -193,20 +193,22 @@ In below table of functions, we have several types that have special means:
193
193
|`pb.encode(type, table, b)`| buffer | encode a message table into binary form to buffer |
194
194
|`pb.decode(type, data)`| table | decode a binary message into Lua table |
195
195
|`pb.decode(type, data, table)`| table | decode a binary message into a given Lua table |
196
-
|`pb.pack(fmt, ...)`| string |same as `buffer.pack()` but return string |
197
-
|`pb.unpack(data, fmt, ...)`| values... |same as `slice.unpack()` but accept data |
196
+
|`pb.pack(type, ...)`| string |encode a message with flatten fields (ordered by field number)|
197
+
|`pb.unpack(data, type, ...)`| values... |decode a message with flatten fields (just like above)|
198
198
|`pb.types()`| iterator | iterate all types in `pb` module |
199
199
|`pb.type(type)`| see below | return informations for specific type |
200
200
|`pb.fields(type)`| iterator | iterate all fields in a message |
201
201
|`pb.field(type, string)`| see below | return informations for specific field of type |
202
-
|`pb.typefmt(type)`|String | transform type name of field into pack/unpack formatter |
202
+
|`pb.typefmt(type)`|string| transform type name of field into pack/unpack formatter |
203
203
|`pb.enum(type, string)`| number | get the value of a enum by name |
204
204
|`pb.enum(type, number)`| string | get the name of a enum by value |
205
205
|`pb.defaults(type[, table/nil])`| table | get the default table of type |
206
206
|`pb.hook(type[, function])`| function | get or set hook functions |
207
+
|`pb.encode_hook(type[, function])`| function | get or set encode hook functions |
207
208
|`pb.option(string)`| string | set options to decoder/encoder |
208
209
|`pb.state()`|`pb.State`| retrieve current pb state |
209
210
|`pb.state(newstate \| nil)`|`pb.State`| set new pb state and retrieve the old one |
211
+
|`pb.tohex(string)`| string | encode string as hexadigits, for debug purposes |
210
212
211
213
#### Schema loading
212
214
@@ -318,6 +320,10 @@ local function make_hook(name, func)
318
320
end
319
321
```
320
322
323
+
You could enable “encode hooking” function by call `pb.option “enable_enchooks”`. encode hooks will be called **before** any message or enum value been encoded. The hook caller only accepts one value, the comming encoding value, and can returns a new value that instead to be encoded. The hook could returns `nil` or just not return anything to makes `pb.encode` just encode the original value.
324
+
325
+
You could setup encode hooks by `pb.encode_hook()` routine, it’s just as same as `pb.hook()`, but for getting/setting the encode hooks.
326
+
321
327
#### Options
322
328
323
329
Setting options to change the behavior of other routines.
@@ -335,15 +341,17 @@ These options are supported currently:
335
341
|`use_default_values`| set default values by copy values from default table before decode |
336
342
|`use_default_metatable`| set default values by set table from `pb.default()` as the metatable |
337
343
|`enable_hooks`|`pb.decode` will call `pb.hooks()` hook functions |
338
-
|`disable_hooks`|`pb.decode` do not call hooks **(default)**|
339
-
|`encode_default_values`| default values also encode |
344
+
|`disable_hooks`|`pb.decode` do not call hooks **(default)**|
345
+
|`enable_enchooks`|`pb.encode` will call `pb.encode_hook()` hook functions |
346
+
|`disable_enchooks`| do not call hooks when encoding messages **(default)**|
347
+
|`encode_default_values`|`pb.encode` encode the default value into the wire format |
340
348
|`no_encode_default_values`| do not encode default values **(default)**|
341
349
|`decode_default_array`| work with `no_default_values`,decode null to empty table for array |
342
350
|`no_decode_default_array`| work with `no_default_values`,decode null to nil for array **(default)**|
343
-
|`encode_order`|guarantees the same message will be encoded into the same result with the same schema and the same data (but the order itself is not specified)|
351
+
|`encode_order`|`pb.encode` encode the messages with field number order |
344
352
|`no_encode_order`| do not have guarantees about encode orders **(default)**|
345
-
|`decode_default_message`| decode null message to default message table |
346
-
|`no_decode_default_message`| decode null message to null **(default)**|
353
+
|`decode_default_message`|`pb.decode` decode the empty messages as a empty table |
354
+
|`no_decode_default_message`|`pb.decode` decode the empty messages as `nil`**(default)**|
347
355
348
356
*Note*: The string returned by `int64_as_string` or `int64_as_hexstring` will prefix a `'#'` character. Because Lua may convert between string with number, prefix a `'#'` makes Lua return the string as-is.
0 commit comments