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
+15-8Lines changed: 15 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -193,20 +193,21 @@ 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
207
|`pb.option(string)`| string | set options to decoder/encoder |
208
208
|`pb.state()`|`pb.State`| retrieve current pb state |
209
209
|`pb.state(newstate \| nil)`|`pb.State`| set new pb state and retrieve the old one |
210
+
|`pb.tohex(string)`| string | encode string as hexadigits, for debug purposes |
210
211
211
212
#### Schema loading
212
213
@@ -318,6 +319,10 @@ local function make_hook(name, func)
318
319
end
319
320
```
320
321
322
+
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.
323
+
324
+
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.
325
+
321
326
#### Options
322
327
323
328
Setting options to change the behavior of other routines.
@@ -335,15 +340,17 @@ These options are supported currently:
335
340
|`use_default_values`| set default values by copy values from default table before decode |
336
341
|`use_default_metatable`| set default values by set table from `pb.default()` as the metatable |
337
342
|`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 |
343
+
|`disable_hooks`|`pb.decode` do not call hooks **(default)**|
344
+
|`enable_enchooks`|`pb.encode` will call `pb.encode_hook()` hook functions |
345
+
|`disable_enchooks`| do not call hooks when encoding messages **(default)**|
346
+
|`encode_default_values`|`pb.encode` encode the default value into the wire format |
340
347
|`no_encode_default_values`| do not encode default values **(default)**|
341
348
|`decode_default_array`| work with `no_default_values`,decode null to empty table for array |
342
349
|`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)|
350
+
|`encode_order`|`pb.encode` encode the messages with field number order |
344
351
|`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)**|
352
+
|`decode_default_message`|`pb.decode` decode the empty messages as a empty table |
353
+
|`no_decode_default_message`|`pb.decode` decode the empty messages as `nil`**(default)**|
347
354
348
355
*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