Custom Record and Free-Form Field Parser
Problem statement
Parse a compact record with five top-level fields:
machineCode,timestamp,field1,field2,$[key:value,key:[item,item],...]&Whitespace is insignificant. Commas inside a bracketed list do not split fields. machineEnum contains code=name entries and always maps the machine code. keyMap contains optional sourceKey=objectKey entries; an unmapped free-form key keeps its original name.
Return machine_code=enumName, the three remaining fixed fields, and every free-form entry in input order as mappedKey=value. Retain repeated keys and bracketed list values.
Function
parseRecord(encodedRecord: String, machineEnum: String[], keyMap: String[]) → String[]Examples
Example 1
encodedRecord = "123,1700,AAA,BBB,$[v1:SSS,v2:[x,y],v1:TTT]&"machineEnum = ["123=TYPE1"]keyMap = ["v1=name","v2=tags"]return = ["machine_code=TYPE1","timestamp=1700","field1=AAA","field2=BBB","name=SSS","tags=[x,y]","name=TTT"]The list comma stays inside v2 and both v1 entries are retained.
Example 2
encodedRecord = "7,9,A,B,$[raw:ok]&"machineEnum = ["7=TYPE7"]keyMap = []return = ["machine_code=TYPE7","timestamp=9","field1=A","field2=B","raw=ok"]An unmapped free-form key is preserved.
Example 3
encodedRecord = "5, 10, X, Y, $[a: [1, 2, 3]]&"machineEnum = ["5=FIVE"]keyMap = ["a=values"]return = ["machine_code=FIVE","timestamp=10","field1=X","field2=Y","values=[1,2,3]"]Insignificant whitespace is removed.
Constraints
- The record is valid and contains exactly four fixed fields plus one
$[...]&block. - Free-form values are nonempty scalars without delimiters or one non-nested bracketed list.
- Mapping keys and values contain none of
,:=[]&or whitespace.