go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/proto/msgpackpb/luagen/examplepb/msg.lua (about) 1 local PB = {} 2 3 local next = next 4 local type = type 5 6 PB.E = { 7 ["go.chromium.org.luci.common.proto.msgpackpb.luagen.examplepb.VALUE"] = { 8 ["ZERO"] = 0, 9 [0] = "ZERO", 10 ["ONE"] = 1, 11 [1] = "ONE", 12 ["TWO"] = 2, 13 [2] = "TWO", 14 }, 15 } 16 17 PB.M = { 18 ["go.chromium.org.luci.common.proto.msgpackpb.luagen.examplepb.TestMessage"] = { 19 marshal = function(obj) 20 local acc, val, T = {}, nil, nil, nil 21 22 val = obj["boolval"] -- 2: bool 23 if val ~= false then 24 local T = type(val) 25 if T ~= "boolean" then 26 error("field boolval: expected boolean, but got "..T) 27 end 28 acc[2] = val 29 end 30 31 val = obj["intval"] -- 3: int64 32 if val ~= 0 then 33 local T = type(val) 34 if T ~= "number" then 35 error("field intval: expected number, but got "..T) 36 end 37 if val > 9007199254740991 then 38 error("field intval: overflows lua max integer") 39 end 40 if val < -9007199254740991 then 41 error("field intval: underflows lua min integer") 42 end 43 acc[3] = val 44 end 45 46 val = obj["uintval"] -- 4: uint64 47 if val ~= 0 then 48 local T = type(val) 49 if T ~= "number" then 50 error("field uintval: expected number, but got "..T) 51 end 52 if val < 0 then 53 error("field uintval: negative") 54 end 55 if val > 9007199254740991 then 56 error("field uintval: overflows lua max integer") 57 end 58 acc[4] = val 59 end 60 61 val = obj["short_intval"] -- 5: int32 62 if val ~= 0 then 63 local T = type(val) 64 if T ~= "number" then 65 error("field short_intval: expected number, but got "..T) 66 end 67 if val > 2147483647 then 68 error("field short_intval: overflows int32") 69 end 70 if val < -2147483648 then 71 error("field short_intval: underflows int32") 72 end 73 acc[5] = val 74 end 75 76 val = obj["short_uintval"] -- 6: uint32 77 if val ~= 0 then 78 local T = type(val) 79 if T ~= "number" then 80 error("field short_uintval: expected number, but got "..T) 81 end 82 if val < 0 then 83 error("field short_uintval: negative") 84 end 85 if val > 4294967295 then 86 error("field short_uintval: overflows max uint32") 87 end 88 acc[6] = val 89 end 90 91 val = obj["strval"] -- 7: string 92 if val ~= "" then 93 local T = type(val) 94 if T ~= "string" then 95 error("field strval: expected string, but got "..T) 96 end 97 acc[7] = val 98 end 99 100 val = obj["floatval"] -- 8: double 101 if val ~= 0 then 102 local T = type(val) 103 if T ~= "number" then 104 error("field floatval: expected number, but got "..T) 105 end 106 acc[8] = val 107 end 108 109 val = obj["short_floatval"] -- 9: float 110 if val ~= 0 then 111 local T = type(val) 112 if T ~= "number" then 113 error("field short_floatval: expected number, but got "..T) 114 end 115 acc[9] = val 116 end 117 118 val = obj["value"] -- 10: enum go.chromium.org.luci.common.proto.msgpackpb.luagen.examplepb.VALUE 119 if val ~= 0 and val ~= "ZERO" then 120 local T = type(val) 121 local origval = val 122 if T == "string" then 123 val = PB.E["go.chromium.org.luci.common.proto.msgpackpb.luagen.examplepb.VALUE"][val] 124 if val == nil then 125 error("field value: bad string enum value "..origval) 126 end 127 elseif T == "number" then 128 if PB.E["go.chromium.org.luci.common.proto.msgpackpb.luagen.examplepb.VALUE"][val] == nil then 129 error("field value: bad numeric enum value "..origval) 130 end 131 else 132 error("field value: expected number or string, but got "..T) 133 end 134 acc[10] = val 135 end 136 137 val = obj["mapfield"] -- 11: map<string, go.chromium.org.luci.common.proto.msgpackpb.luagen.examplepb.TestMessage> 138 if next(val) ~= nil then 139 local T = type(val) 140 if T ~= "table" then 141 error("field mapfield: expected map<string, message>, but got "..T) 142 end 143 local i = 0 144 for k, v in next, val do 145 local T = type(k) 146 if T ~= "string" then 147 error("field mapfield["..i.."th entry]: expected string, but got "..T) 148 end 149 local T = type(v) 150 if T ~= "table" then 151 error("field mapfield["..k.."]: expected table, but got "..T) 152 end 153 if not v["$type"] then 154 error("field mapfield["..k.."]: missing type") 155 end 156 if v["$type"] ~= "go.chromium.org.luci.common.proto.msgpackpb.luagen.examplepb.TestMessage" then 157 error("field mapfield["..k.."]: expected message type 'go.chromium.org.luci.common.proto.msgpackpb.luagen.examplepb.TestMessage', but got "..v["$type"]) 158 end 159 val[k] = PB.M["go.chromium.org.luci.common.proto.msgpackpb.luagen.examplepb.TestMessage"].marshal(v) 160 i = i + 1 161 end 162 acc[11] = val 163 end 164 165 val = obj["duration"] -- 12: google.protobuf.Duration 166 if val ~= nil then 167 local T = type(val) 168 if T ~= "table" then 169 error("field duration: expected table, but got "..T) 170 end 171 if not val["$type"] then 172 error("field duration: missing type") 173 end 174 if val["$type"] ~= "google.protobuf.Duration" then 175 error("field duration: expected message type 'google.protobuf.Duration', but got "..val["$type"]) 176 end 177 val = PB.M["google.protobuf.Duration"].marshal(val) 178 acc[12] = val 179 end 180 181 val = obj["strings"] -- 13: repeated string 182 if next(val) ~= nil then 183 local T = type(val) 184 if T ~= "table" then 185 error("field strings: expected list[string], but got "..T) 186 end 187 local maxIdx = 0 188 local length = 0 189 for i, v in next, val do 190 if type(i) ~= "number" then 191 error("field strings: expected list[string], but got table") 192 end 193 local T = type(v) 194 if T ~= "string" then 195 error("field strings["..(i-1).."]: expected string, but got "..T) 196 end 197 if i > maxIdx then 198 maxIdx = i 199 end 200 length = length + 1 201 end 202 if length ~= maxIdx then 203 error("field strings: expected list[string], but got table") 204 end 205 acc[13] = val 206 end 207 208 val = obj["single_recurse"] -- 14: go.chromium.org.luci.common.proto.msgpackpb.luagen.examplepb.TestMessage 209 if val ~= nil then 210 local T = type(val) 211 if T ~= "table" then 212 error("field single_recurse: expected table, but got "..T) 213 end 214 if not val["$type"] then 215 error("field single_recurse: missing type") 216 end 217 if val["$type"] ~= "go.chromium.org.luci.common.proto.msgpackpb.luagen.examplepb.TestMessage" then 218 error("field single_recurse: expected message type 'go.chromium.org.luci.common.proto.msgpackpb.luagen.examplepb.TestMessage', but got "..val["$type"]) 219 end 220 val = PB.M["go.chromium.org.luci.common.proto.msgpackpb.luagen.examplepb.TestMessage"].marshal(val) 221 acc[14] = val 222 end 223 224 val = obj["multi_recursion"] -- 15: repeated go.chromium.org.luci.common.proto.msgpackpb.luagen.examplepb.TestMessage 225 if next(val) ~= nil then 226 local T = type(val) 227 if T ~= "table" then 228 error("field multi_recursion: expected list[message], but got "..T) 229 end 230 local maxIdx = 0 231 local length = 0 232 for i, v in next, val do 233 if type(i) ~= "number" then 234 error("field multi_recursion: expected list[message], but got table") 235 end 236 local T = type(v) 237 if T ~= "table" then 238 error("field multi_recursion["..(i-1).."]: expected table, but got "..T) 239 end 240 if not v["$type"] then 241 error("field multi_recursion["..(i-1).."]: missing type") 242 end 243 if v["$type"] ~= "go.chromium.org.luci.common.proto.msgpackpb.luagen.examplepb.TestMessage" then 244 error("field multi_recursion["..(i-1).."]: expected message type 'go.chromium.org.luci.common.proto.msgpackpb.luagen.examplepb.TestMessage', but got "..v["$type"]) 245 end 246 val[i] = PB.M["go.chromium.org.luci.common.proto.msgpackpb.luagen.examplepb.TestMessage"].marshal(v) 247 if i > maxIdx then 248 maxIdx = i 249 end 250 length = length + 1 251 end 252 if length ~= maxIdx then 253 error("field multi_recursion: expected list[message], but got table") 254 end 255 acc[15] = val 256 end 257 258 local unknown = obj["$unknown"] 259 if unknown ~= nil then 260 for k, v in next, unknown do acc[k] = v end 261 end 262 return acc 263 end, 264 265 unmarshal = function(raw) 266 local defaults = {} 267 local ret = { 268 ["$unknown"] = {}, 269 ["$type"] = "go.chromium.org.luci.common.proto.msgpackpb.luagen.examplepb.TestMessage", 270 ["boolval"] = false, 271 ["intval"] = 0, 272 ["uintval"] = 0, 273 ["short_intval"] = 0, 274 ["short_uintval"] = 0, 275 ["strval"] = "", 276 ["floatval"] = 0, 277 ["short_floatval"] = 0, 278 ["value"] = "ZERO", 279 ["mapfield"] = {}, 280 ["duration"] = nil, 281 ["strings"] = {}, 282 ["single_recurse"] = nil, 283 ["multi_recursion"] = {}, 284 } 285 local dec = { 286 [2] = function(val) -- boolval: bool 287 local T = type(val) 288 if T ~= "boolean" then 289 error("field boolval: expected boolean, but got "..T) 290 end 291 ret["boolval"] = val 292 end, 293 [3] = function(val) -- intval: int64 294 local T = type(val) 295 if T ~= "number" then 296 error("field intval: expected number, but got "..T) 297 end 298 ret["intval"] = val 299 end, 300 [4] = function(val) -- uintval: uint64 301 local T = type(val) 302 if T ~= "number" then 303 error("field uintval: expected number, but got "..T) 304 end 305 ret["uintval"] = val 306 end, 307 [5] = function(val) -- short_intval: int32 308 local T = type(val) 309 if T ~= "number" then 310 error("field short_intval: expected number, but got "..T) 311 end 312 ret["short_intval"] = val 313 end, 314 [6] = function(val) -- short_uintval: uint32 315 local T = type(val) 316 if T ~= "number" then 317 error("field short_uintval: expected number, but got "..T) 318 end 319 ret["short_uintval"] = val 320 end, 321 [7] = function(val) -- strval: string 322 local T = type(val) 323 if T == "number" then 324 if not PB.internUnmarshalTable then 325 error("field strval: failed to look up interned string: intern table not set") 326 end 327 local origval = val 328 local newval = PB.internUnmarshalTable[val] 329 if newval == nil then 330 error("field strval: failed to look up interned string: "..origval) 331 end 332 val = newval 333 T = type(val) 334 end 335 if T ~= "string" then 336 error("field strval: expected string, but got "..T) 337 end 338 ret["strval"] = val 339 end, 340 [8] = function(val) -- floatval: double 341 local T = type(val) 342 if T ~= "number" then 343 error("field floatval: expected number, but got "..T) 344 end 345 ret["floatval"] = val 346 end, 347 [9] = function(val) -- short_floatval: float 348 local T = type(val) 349 if T ~= "number" then 350 error("field short_floatval: expected number, but got "..T) 351 end 352 ret["short_floatval"] = val 353 end, 354 [10] = function(val) -- value: enum go.chromium.org.luci.common.proto.msgpackpb.luagen.examplepb.VALUE 355 local T = type(val) 356 if T ~= "number" then 357 error("field value: expected numeric enum, but got "..T) 358 end 359 local origval = val 360 local newval = PB.E["go.chromium.org.luci.common.proto.msgpackpb.luagen.examplepb.VALUE"][val] 361 if newval == nil then 362 error("field value: bad enum value "..origval) 363 end 364 ret["value"] = newval 365 end, 366 [11] = function(val) -- mapfield: map<string, go.chromium.org.luci.common.proto.msgpackpb.luagen.examplepb.TestMessage> 367 local T = type(val) 368 if T ~= "table" then 369 error("field mapfield: expected map<string, message>, but got "..T) 370 end 371 local i = 0 372 for k, v in next, val do 373 local T = type(k) 374 if T == "number" then 375 if not PB.internUnmarshalTable then 376 error("field mapfield["..i.."th entry]: failed to look up interned string: intern table not set") 377 end 378 local origval = k 379 local newval = PB.internUnmarshalTable[k] 380 if newval == nil then 381 error("field mapfield["..i.."th entry]: failed to look up interned string: "..origval) 382 end 383 val = newval 384 T = type(val) 385 end 386 if T ~= "string" then 387 error("field mapfield["..i.."th entry]: expected string, but got "..T) 388 end 389 k = val 390 local T = type(v) 391 if T ~= "table" then 392 error("field mapfield["..k.."]: expected table, but got "..T) 393 end 394 val[k] = PB.M["go.chromium.org.luci.common.proto.msgpackpb.luagen.examplepb.TestMessage"].unmarshal(v) 395 i = i + 1 396 end 397 ret["mapfield"] = val 398 end, 399 [12] = function(val) -- duration: google.protobuf.Duration 400 local T = type(val) 401 if T ~= "table" then 402 error("field duration: expected table, but got "..T) 403 end 404 ret["duration"] = PB.M["google.protobuf.Duration"].unmarshal(val) 405 end, 406 [13] = function(val) -- strings: repeated string 407 local T = type(val) 408 if T ~= "table" then 409 error("field strings: expected list[string], but got "..T) 410 end 411 local max = 0 412 local count = 0 413 for i, v in next, val do 414 if type(i) ~= "number" then 415 error("field strings: expected list[string], but got table") 416 end 417 if i > max then 418 max = i 419 end 420 count = count + 1 421 local T = type(v) 422 if T == "number" then 423 if not PB.internUnmarshalTable then 424 error("field strings["..(i-1).."]: failed to look up interned string: intern table not set") 425 end 426 local origval = v 427 local newval = PB.internUnmarshalTable[v] 428 if newval == nil then 429 error("field strings["..(i-1).."]: failed to look up interned string: "..origval) 430 end 431 val = newval 432 T = type(val) 433 end 434 if T ~= "string" then 435 error("field strings["..(i-1).."]: expected string, but got "..T) 436 end 437 val[i] = val 438 end 439 if max ~= count then 440 error("field strings: expected list[string], but got table") 441 end 442 ret["strings"] = val 443 end, 444 [14] = function(val) -- single_recurse: go.chromium.org.luci.common.proto.msgpackpb.luagen.examplepb.TestMessage 445 local T = type(val) 446 if T ~= "table" then 447 error("field single_recurse: expected table, but got "..T) 448 end 449 ret["single_recurse"] = PB.M["go.chromium.org.luci.common.proto.msgpackpb.luagen.examplepb.TestMessage"].unmarshal(val) 450 end, 451 [15] = function(val) -- multi_recursion: repeated go.chromium.org.luci.common.proto.msgpackpb.luagen.examplepb.TestMessage 452 local T = type(val) 453 if T ~= "table" then 454 error("field multi_recursion: expected list[message], but got "..T) 455 end 456 local max = 0 457 local count = 0 458 for i, v in next, val do 459 if type(i) ~= "number" then 460 error("field multi_recursion: expected list[message], but got table") 461 end 462 if i > max then 463 max = i 464 end 465 count = count + 1 466 local T = type(v) 467 if T ~= "table" then 468 error("field multi_recursion["..(i-1).."]: expected table, but got "..T) 469 end 470 val[i] = PB.M["go.chromium.org.luci.common.proto.msgpackpb.luagen.examplepb.TestMessage"].unmarshal(v) 471 end 472 if max ~= count then 473 error("field multi_recursion: expected list[message], but got table") 474 end 475 ret["multi_recursion"] = val 476 end, 477 } 478 for k, v in next, raw do 479 local fn = dec[k] 480 if fn ~= nil then 481 fn(v) 482 else 483 ret["$unknown"][k] = v 484 end 485 end 486 return ret 487 end, 488 keys = { 489 ["boolval"] = true, 490 ["intval"] = true, 491 ["uintval"] = true, 492 ["short_intval"] = true, 493 ["short_uintval"] = true, 494 ["strval"] = true, 495 ["floatval"] = true, 496 ["short_floatval"] = true, 497 ["value"] = true, 498 ["mapfield"] = true, 499 ["duration"] = true, 500 ["strings"] = true, 501 ["single_recurse"] = true, 502 ["multi_recursion"] = true, 503 }, 504 }, 505 506 ["google.protobuf.Duration"] = { 507 marshal = function(obj) 508 local acc, val, T = {}, nil, nil, nil 509 510 val = obj["seconds"] -- 1: int64 511 if val ~= 0 then 512 local T = type(val) 513 if T ~= "number" then 514 error("field seconds: expected number, but got "..T) 515 end 516 if val > 9007199254740991 then 517 error("field seconds: overflows lua max integer") 518 end 519 if val < -9007199254740991 then 520 error("field seconds: underflows lua min integer") 521 end 522 acc[1] = val 523 end 524 525 val = obj["nanos"] -- 2: int32 526 if val ~= 0 then 527 local T = type(val) 528 if T ~= "number" then 529 error("field nanos: expected number, but got "..T) 530 end 531 if val > 2147483647 then 532 error("field nanos: overflows int32") 533 end 534 if val < -2147483648 then 535 error("field nanos: underflows int32") 536 end 537 acc[2] = val 538 end 539 540 local unknown = obj["$unknown"] 541 if unknown ~= nil then 542 for k, v in next, unknown do acc[k] = v end 543 end 544 return acc 545 end, 546 547 unmarshal = function(raw) 548 local defaults = {} 549 local ret = { 550 ["$unknown"] = {}, 551 ["$type"] = "google.protobuf.Duration", 552 ["seconds"] = 0, 553 ["nanos"] = 0, 554 } 555 local dec = { 556 [1] = function(val) -- seconds: int64 557 local T = type(val) 558 if T ~= "number" then 559 error("field seconds: expected number, but got "..T) 560 end 561 ret["seconds"] = val 562 end, 563 [2] = function(val) -- nanos: int32 564 local T = type(val) 565 if T ~= "number" then 566 error("field nanos: expected number, but got "..T) 567 end 568 ret["nanos"] = val 569 end, 570 } 571 for k, v in next, raw do 572 local fn = dec[k] 573 if fn ~= nil then 574 fn(v) 575 else 576 ret["$unknown"][k] = v 577 end 578 end 579 return ret 580 end, 581 keys = { 582 ["seconds"] = true, 583 ["nanos"] = true, 584 }, 585 }, 586 } 587 588 if cmsgpack == nil then 589 local cmsgpack = ... 590 assert(cmsgpack) 591 end 592 local cmsgpack_pack = cmsgpack.pack 593 local cmsgpack_unpack = cmsgpack.unpack 594 function PB.setInternTable(t) 595 if PB.internUnmarshalTable then 596 error("cannot set intern table twice") 597 end 598 if type(t) ~= "table" then 599 error("must call PB.setInternTable with a table (got "..type(t)..")") 600 end 601 PB.internUnmarshalTable = {} 602 for i, v in ipairs(t) do 603 PB.internUnmarshalTable[i-1] = v 604 end 605 end 606 function PB.marshal(obj) 607 local T = obj["$type"] 608 if T == nil then 609 error("obj is missing '$type' field") 610 end 611 local codec = PB.M[T] 612 if codec == nil then 613 error("unknown proto message type: "..T) 614 end 615 return cmsgpack_pack(codec.marshal(obj)) 616 end 617 function PB.unmarshal(messageName, msgpackpb) 618 local codec = PB.M[messageName] 619 if codec == nil then 620 error("unknown proto message type: "..messageName) 621 end 622 return codec.unmarshal(cmsgpack_unpack(msgpackpb)) 623 end 624 function PB.new(messageName, defaults) 625 local codec = PB.M[messageName] 626 if codec == nil then 627 error("unknown proto message type: "..messageName) 628 end 629 local ret = codec.unmarshal({}) 630 if defaults ~= nil then 631 for k, v in next, defaults do 632 if k[0] ~= "$" then 633 if not codec.keys[k] then 634 error("invalid property name '"..k.."' for "..messageName) 635 end 636 ret[k] = v 637 end 638 end 639 end 640 return ret 641 end 642 return PB