@@ -131,46 +131,48 @@ local json = require("src.lua.json")
131131--- @alias PayloadFieldValue string | number | boolean
132132--- @alias PayloadValue table<FieldName , PayloadFieldValue>
133133
134- --- @class Field
135- --- @field name string
136- --- @field type FieldType
137- --- @field num_of_bits ? integer
138- --- @field unsigned ? boolean
139- --- @field minimum ? number
140- --- @field maximum ? number
141-
142- --- @class Schema
143- --- @field version integer
144- --- @field name string
145- --- @field use_base ? boolean
146- --- @field allow_override ? boolean
147- --- @field fields Field[]
148-
149- --- @class Payload
150- --- @field schema_name string
151- --- @field payload table<PayloadValue>
152-
153- --- @class BaseSchemaConfig
154- --- @field enabled boolean
155-
156- --- @class W3CDataConfig
157- --- @field base_schema BaseSchemaConfig
158-
159- --- @class Chunk
160- --- @field id integer
161- --- @field index integer
162- --- @field count integer
163- --- @field payload string
164-
134+ --- @class Field Field description for a schema.
135+ --- @field name string Name of the field.
136+ --- @field type FieldType Type of the field. Decimal numbers use ` float ` . ` number ` is used only when using ` minimum ` and ` maxiumum `
137+ --- @field num_of_bits ? integer Number of bits used by this field. Requires ` type = "int" ` if used. Only valid for integer types.
138+ --- @field unsigned ? boolean Whether the field is unsigned or not. Only applies to ` byte ` , ` short ` and ` int ` types
139+ --- @field minimum ? number Minimum number for a field , only used when ` tyoe = "number" `
140+ --- @field maximum ? number Maximum number for a field , only used when ` type = "number" ` `
141+
142+ --- @class Schema Schema used for an event
143+ --- @field version integer Version of the schema
144+ --- @field name string Name of the schema
145+ --- @field use_base ? boolean Whether this schema should include the " base " schema fields or not. Defaults to ` false `
146+ --- @field fields Field[] All fields for this schema
147+
148+ --- @class Payload Payload that maps a schema name to a table when being packed.
149+ --- @field schema_name string Name of the schema that the payload is for
150+ --- @field payload table<PayloadValue> Table containing the payload values
151+
152+ --- @class BaseSchemaConfig Config for W3CData base schema
153+ --- @field enabled boolean Whether using the base schema is enabled or not
154+
155+ --- @class W3CDataConfig Config for W3CData
156+ --- @field base_schema BaseSchemaConfig Config for base schema
157+
158+ --- @class Chunk Chunk of a payload , for when a single payload is too large to be sent at once
159+ --- @field id integer Unique ID for a payload. All chunks for a single payload will share the same id
160+ --- @field index integer Index of the chunk in the payload.
161+ --- @field count integer Count of all chunks in the payload
162+ --- @field payload string Payload for this chunk. Combine the payload from all chunks with the same id before decoding
163+
164+ -- Header byte values used in the payload to identify what the payload is
165165local HEADER_VALUES = {
166166 EVENT = 0x01 ,
167167 CHECKSUM = 0x02 ,
168168
169169 CHUNK = 0x80 ,
170170}
171171
172+ -- Used to mask numbers to 32 bit integers as bit shifting can cause bits to go over 32 bits boundary
172173local INT_MASK = 0xFFFFFFFF
173174
175+ -- Limits for integers, used to validate values are within the exepcted number range
174176local LIMITS = {
175177 BYTE = {
176178 SIGNED_LO = (- 2 << 7 ),
@@ -207,8 +209,11 @@ local INTERNAL_SCHEMA_NAMES = {
207209--- @field schemas table<integer , Schema>
208210local W3CData = {
209211 config = { base_schema = { enabled = true } },
212+ -- Base schemas required for basic functionality. DO NOT REMOVE or things will break.
213+ -- The order needs to match the indexes specified in `INTERNAL_SCHEMA_ID`, as they're used as indexes to this table.
210214 schemas = {
211215 {
216+ -- Used to send all schemas after being registered so that they can be used in parsing
212217 version = 1 ,
213218 name = INTERNAL_SCHEMA_NAMES .SCHEMA_REGISTRY ,
214219 fields = {
@@ -217,17 +222,24 @@ local W3CData = {
217222 { name = " schemas_json" , type = " string" },
218223 },
219224 },
225+ -- Used for sending checksums.
220226 { version = 1 , name = INTERNAL_SCHEMA_NAMES .CHECKSUM , fields = { { name = " checksum" , type = " string" } } },
227+ -- Base empty schema as the library has specific handling that expects this to exist.
221228 { version = 0 , name = INTERNAL_SCHEMA_NAMES .BASE , fields = {} },
222229 },
223230}
224231
232+ -- Mapping of names to ids, used for lookup.
233+ -- Schema IDs are used for compressed data as they're much smaller than the string names.
225234local schema_name_to_id_mapping = {
226235 schema_registry = INTERNAL_SCHEMA_ID .SCHEMA ,
227236 checksum = INTERNAL_SCHEMA_ID .CHECKSUM ,
228237 base = INTERNAL_SCHEMA_ID .BASE ,
229238}
230- ---
239+
240+ --- Parses a "number" type field and ensures that the minimum and maximum values are valid if set.
241+ --- Sets the field.type to `byte`, `short` or `int` depending on the minimum and maximum values.
242+ --- Sets `field.unsigned` if the minimum is >= 0
231243--- @param field Field
232244local function parse_number_field (field )
233245 if field .type ~= " number" then
342354--- Register a schema to be used for compression and decompression.
343355--- Schemas with the name "base" will be combined with all other schemas if `config.base_schema.enabled = true` and
344356--- the `schema.use_base = true`
345- --- Using a base schema is enabled by default for registered schemas.
357+ --- Using a base schema is disabled by default for registered schemas.
346358--- @param schema Schema Schema to be registered
347359function W3CData :register_schema (schema )
348360 assert (schema .name , " Schemas require a name to be set" )
@@ -360,13 +372,13 @@ function W3CData:register_schema(schema)
360372 return
361373 end
362374
363- if schema_name_to_id_mapping [schema .name ] and not schema . allow_override then
364- -- If schema already exists and is configured to not allow overriding, return.
375+ if schema_name_to_id_mapping [schema .name ] then
376+ -- If schema already exists just return. Schemas should only be registered once, we don't allow overriding
365377 return
366378 end
367379
368380 local schema_id = # self .schemas + 1
369- schema .use_base = schema .use_base or true
381+ schema .use_base = schema .use_base or false
370382
371383 self .schemas [schema_id ] = schema
372384 schema_name_to_id_mapping [schema .name ] = schema_id
@@ -395,14 +407,20 @@ function W3CData:get_schema(schema_name)
395407 return self :get_schema_by_id (id )
396408end
397409
410+ --- Checks whether a schema exists and has been registered.
411+ --- @param schema_name string Name of the schema to check
412+ --- @return boolean schema_exists True if the schema has been registered , false if not.
398413function W3CData :has_schema (schema_name )
399414 return self :get_schema (schema_name ) and true or false
400415end
401416
417+ --- Checks whether a schema should include the base schema
418+ --- @param schema_name string Name of the schema to check
419+ --- @return boolean should_use_base True if the schema should include the base schema , false if not
402420function W3CData :should_use_base (schema_name )
403421 local schema = self :get_schema (schema_name )
404422
405- return self .config .base_schema .enabled and schema .use_base
423+ return ( self .config .base_schema .enabled and schema .use_base ) and true or false
406424end
407425
408426--- COBS encodes a string to remove null bytes so that it can be safely sent using BlzSendSyncData.
@@ -466,10 +484,9 @@ function W3CData.cobs_decode(input)
466484 return table.concat (output )
467485end
468486
469- --- Combines a schema with a base schema, if the base schema config is enabled, a base schema exists and the target schema
470- --- has `use_base = true`
471- --- @param schema_id integer id for the schema to combine with the base schema
472- --- @return Schema Schema Combined base and target schema
487+ --- Gets a schema by id. Includes the base schema fields if configured.
488+ --- @param schema_id integer Id of the schema to get
489+ --- @return Schema Schema Schema including base schema fields if configured
473490function W3CData :get_schema_by_id (schema_id )
474491 if not self .config .base_schema .enabled then
475492 return self .schemas [schema_id ] or {}
@@ -486,7 +503,6 @@ function W3CData:get_schema_by_id(schema_id)
486503 version = specific .version ,
487504 name = specific .name ,
488505 use_base = specific .use_base ,
489- allow_override = specific .allow_override ,
490506 fields = {},
491507 }
492508
@@ -1082,18 +1098,23 @@ function W3CData:generate_checksum_payload(crc)
10821098 return string.char (HEADER_VALUES .CHECKSUM ) .. packed
10831099end
10841100
1101+ --- Generate payloads containing schema registry. Used so that the schemas can be sent using BlzSendSyncData and
1102+ --- used to decode and decompress payloads during parsing
1103+ --- @return table<string> payloads String payloads to send using BlzSendSyncData
10851104function W3CData :generate_registry_payloads ()
10861105 local schema_payload = {}
10871106 for _ , schema in ipairs (self .schemas ) do
10881107 -- Use function so it applies base schema if needed
10891108 schema_payload [# schema_payload + 1 ] = self :get_schema (schema .name )
10901109 end
1091- local event = { {
1092- schema_name = " schema_registry" ,
1093- payload = {
1094- json .encode (schema_payload ),
1110+ local event = {
1111+ {
1112+ schema_name = INTERNAL_SCHEMA_NAMES .SCHEMA_REGISTRY ,
1113+ payload = {
1114+ json .encode (schema_payload ),
1115+ },
10951116 },
1096- } }
1117+ }
10971118 local encoded , _ = self :encode_payload (event , 180 )
10981119 return encoded
10991120end
0 commit comments