@reifydb/core 0.10.0 → 0.10.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -769,6 +769,7 @@ declare class Option<T> {
769
769
  readonly [OPTION_BRAND] = true;
770
770
  private constructor();
771
771
  static some<T>(value: T): Option<T>;
772
+ static from<T>(value: T | null | undefined, inner: Type): Option<T>;
772
773
  static none<T = never>(inner: Type): Option<T>;
773
774
  isSome(): boolean;
774
775
  isNone(): boolean;
@@ -795,6 +796,18 @@ declare class Utf8Value implements Value {
795
796
  encode(): TypeValuePair;
796
797
  }
797
798
 
799
+ declare class EnumVariant implements Value {
800
+ readonly type: Type;
801
+ readonly enum: string;
802
+ readonly variant: string;
803
+ constructor(enumName: string, variant: string);
804
+ valueOf(): string;
805
+ toString(): string;
806
+ equals(other: Value): boolean;
807
+ toJSON(): string;
808
+ encode(): TypeValuePair;
809
+ }
810
+
798
811
  /**
799
812
  * A UUID version 4 (random) value type
800
813
  */
@@ -1009,6 +1022,7 @@ interface WireType {
1009
1022
  accuracy?: number;
1010
1023
  precision?: number;
1011
1024
  scale?: number;
1025
+ enum?: string;
1012
1026
  }
1013
1027
  declare function typeToWire(type: Type): WireType;
1014
1028
  declare function typeFromWire(wire: WireType): Type;
@@ -1068,12 +1082,16 @@ interface ListType {
1068
1082
  interface RecordType {
1069
1083
  Record: RecordField[];
1070
1084
  }
1071
- type Type = BaseType | OptionType | DigestType | FixedPointType | ListType | RecordType;
1085
+ interface EnumType {
1086
+ Enum: string;
1087
+ }
1088
+ type Type = BaseType | OptionType | DigestType | FixedPointType | ListType | RecordType | EnumType;
1089
+ declare function isEnumType(t: Type): t is EnumType;
1072
1090
  declare function isOptionType(t: Type): t is OptionType;
1073
1091
  declare function isDigestType(t: Type): t is DigestType;
1074
1092
  declare function isListType(t: Type): t is ListType;
1075
1093
  declare function isRecordType(t: Type): t is RecordType;
1076
- declare function unwrapOptionType(t: Type): BaseType | DigestType | FixedPointType | ListType | RecordType;
1094
+ declare function unwrapOptionType(t: Type): BaseType | DigestType | FixedPointType | ListType | RecordType | EnumType;
1077
1095
  declare function optionDepth(t: Type): number;
1078
1096
  declare function innerOfOption(t: OptionType): Type;
1079
1097
  /** A wire cell: a JSON bool for Boolean, a plain string for every other scalar type, or a real JSON array/object for List/Record. */
@@ -1284,6 +1302,7 @@ interface ErrorResponse {
1284
1302
  type: "Err";
1285
1303
  payload: {
1286
1304
  diagnostic: Diagnostic;
1305
+ index?: number;
1287
1306
  };
1288
1307
  }
1289
1308
  declare class ReifyError extends Error {
@@ -1295,7 +1314,8 @@ declare class ReifyError extends Error {
1295
1314
  readonly help?: string;
1296
1315
  readonly notes: string[];
1297
1316
  readonly cause?: Diagnostic;
1298
- constructor(response: ErrorResponse);
1317
+ readonly index?: number;
1318
+ constructor(response: Pick<ErrorResponse, "payload">);
1299
1319
  toString(): string;
1300
1320
  }
1301
1321
  /**
@@ -1355,6 +1375,7 @@ declare const TYPE_CODE: {
1355
1375
  readonly Record: 28;
1356
1376
  readonly Tuple: 29;
1357
1377
  readonly Digest: 30;
1378
+ readonly Enum: 31;
1358
1379
  };
1359
1380
  type TypeName = keyof typeof TYPE_CODE;
1360
1381
  declare function typeNameFromCode(code: number): TypeName;
@@ -1416,4 +1437,4 @@ interface RqlToken {
1416
1437
  }
1417
1438
  declare function tokenizeRql(source: string): RqlToken[];
1418
1439
 
1419
- export { type ArrayShapeNode, type BaseType, BlobValue, BooleanValue, type Column, type ColumnAlign, DECIMAL_DEFAULT_SCALE, DateTimeValue, DateValue, type DecimalType, DecimalValue, type Diagnostic, type DiagnosticColumn, type DigestInnerType, type DigestType, DigestValue, DurationValue, type ErrorResponse, FIXED_POINT_MAX_PRECISION, FIXED_POINT_NARROW_PRECISION, type FixedPointKind, type FixedPointType, Float4Value, Float8Value, type Fragment, type Frame, type FrameResults, IdentityIdValue, type InferShape, type InferShapes, Int16Value, Int1Value, Int2Value, Int4Value, Int8Value, type ListType, ListValue, type MissingPresentation, NONE_PRESENTATION, NONE_VALUE, NoneValue, type ObjectShapeNode, Option, type OptionShapeNode, type OptionType, type Params, type PlannedColumn, type PrimitiveShapeNode, type PrimitiveToTS, type PrimitiveToValue, ROW_NUMBER_KEY, RQL_KEYWORDS, RQL_LITERALS, RQL_OPERATORS, RQL_SYSTEM_COLUMNS, RQL_TYPES, type RecordField, type RecordType, RecordValue, ReifyError, type ResultColumn, type RqlToken, type RqlTokenKind, Shape, ShapeBuilder, ShapeMismatch, type ShapeNode, type SingleFrameResult, TYPE_CODE, type TableLayoutOptions, type TablePlan, TimeValue, type Type, type TypeName, type TypeValuePair, Uint16Value, Uint1Value, Uint2Value, Uint4Value, Uint8Value, Utf8Value, Uuid4Value, Uuid7Value, Value, type ValueRole, type ValueShapeNode, type WireCellValue, type WireType, asFrameResults, cellText, checkFrame, checkFrames, columnsFromWire, columnsToRows, decimalType, decode, digestType, digestTypeName, displayWidth, encodeParams, encodeValue, envelopeToColumns, envelopesToFrames, fixedPointKind, fixedPointPrecision, fixedPointScale, fixedPointType, fixedPointTypeName, formatValue, framesFromWire, graphemeWidth, inferColumns, innerOfOption, isDigestType, isFixedPointType, isListType, isOption, isOptionType, isRecordType, noneDepth, noneMarker, noneMarkerDepth, optionDepth, padToWidth, parseValue, planTable, tokenizeRql, transformFrames, transformResult, truncateToWidth, typeFromWire, typeNameFromCode, typeToWire, unwrapOptionType, validateShape, valueRole, valueTypeName };
1440
+ export { type ArrayShapeNode, type BaseType, BlobValue, BooleanValue, type Column, type ColumnAlign, DECIMAL_DEFAULT_SCALE, DateTimeValue, DateValue, type DecimalType, DecimalValue, type Diagnostic, type DiagnosticColumn, type DigestInnerType, type DigestType, DigestValue, DurationValue, type EnumType, EnumVariant, type ErrorResponse, FIXED_POINT_MAX_PRECISION, FIXED_POINT_NARROW_PRECISION, type FixedPointKind, type FixedPointType, Float4Value, Float8Value, type Fragment, type Frame, type FrameResults, IdentityIdValue, type InferShape, type InferShapes, Int16Value, Int1Value, Int2Value, Int4Value, Int8Value, type ListType, ListValue, type MissingPresentation, NONE_PRESENTATION, NONE_VALUE, NoneValue, type ObjectShapeNode, Option, type OptionShapeNode, type OptionType, type Params, type PlannedColumn, type PrimitiveShapeNode, type PrimitiveToTS, type PrimitiveToValue, ROW_NUMBER_KEY, RQL_KEYWORDS, RQL_LITERALS, RQL_OPERATORS, RQL_SYSTEM_COLUMNS, RQL_TYPES, type RecordField, type RecordType, RecordValue, ReifyError, type ResultColumn, type RqlToken, type RqlTokenKind, Shape, ShapeBuilder, ShapeMismatch, type ShapeNode, type SingleFrameResult, TYPE_CODE, type TableLayoutOptions, type TablePlan, TimeValue, type Type, type TypeName, type TypeValuePair, Uint16Value, Uint1Value, Uint2Value, Uint4Value, Uint8Value, Utf8Value, Uuid4Value, Uuid7Value, Value, type ValueRole, type ValueShapeNode, type WireCellValue, type WireType, asFrameResults, cellText, checkFrame, checkFrames, columnsFromWire, columnsToRows, decimalType, decode, digestType, digestTypeName, displayWidth, encodeParams, encodeValue, envelopeToColumns, envelopesToFrames, fixedPointKind, fixedPointPrecision, fixedPointScale, fixedPointType, fixedPointTypeName, formatValue, framesFromWire, graphemeWidth, inferColumns, innerOfOption, isDigestType, isEnumType, isFixedPointType, isListType, isOption, isOptionType, isRecordType, noneDepth, noneMarker, noneMarkerDepth, optionDepth, padToWidth, parseValue, planTable, tokenizeRql, transformFrames, transformResult, truncateToWidth, typeFromWire, typeNameFromCode, typeToWire, unwrapOptionType, validateShape, valueRole, valueTypeName };
package/dist/index.js CHANGED
@@ -1140,7 +1140,8 @@ var TYPE_CODE = {
1140
1140
  List: 27,
1141
1141
  Record: 28,
1142
1142
  Tuple: 29,
1143
- Digest: 30
1143
+ Digest: 30,
1144
+ Enum: 31
1144
1145
  };
1145
1146
  var CODE_TO_NAME = Object.fromEntries(
1146
1147
  Object.entries(TYPE_CODE).map(([k, v]) => [v, k])
@@ -2639,6 +2640,9 @@ function describeType(t) {
2639
2640
  if (isRecordType(t)) {
2640
2641
  return `Record(${t.Record.map((field) => `${field.name}: ${describeType(field.type)}`).join(", ")})`;
2641
2642
  }
2643
+ if (isEnumType(t)) {
2644
+ return `Enum(${t.Enum})`;
2645
+ }
2642
2646
  return t;
2643
2647
  }
2644
2648
  function hasType(value) {
@@ -2659,8 +2663,14 @@ var _Option = class _Option {
2659
2663
  this[_a] = true;
2660
2664
  }
2661
2665
  static some(value) {
2666
+ if (value === null || value === void 0) {
2667
+ throw new Error("Option.some over null or undefined, use Option.none(inner) or Option.from(value, inner)");
2668
+ }
2662
2669
  return new _Option({ some: true, value });
2663
2670
  }
2671
+ static from(value, inner) {
2672
+ return value === null || value === void 0 ? _Option.none(inner) : _Option.some(value);
2673
+ }
2664
2674
  static none(inner) {
2665
2675
  return new _Option({ some: false, inner });
2666
2676
  }
@@ -2748,6 +2758,33 @@ var Utf8Value = class _Utf8Value {
2748
2758
  }
2749
2759
  };
2750
2760
 
2761
+ // src/value/enum-variant.ts
2762
+ var EnumVariant = class _EnumVariant {
2763
+ constructor(enumName, variant) {
2764
+ if (typeof enumName !== "string" || typeof variant !== "string") {
2765
+ throw new Error(`EnumVariant needs a string enum and variant, got ${typeof enumName} and ${typeof variant}`);
2766
+ }
2767
+ this.type = { Enum: enumName };
2768
+ this.enum = enumName;
2769
+ this.variant = variant;
2770
+ }
2771
+ valueOf() {
2772
+ return this.variant;
2773
+ }
2774
+ toString() {
2775
+ return this.variant;
2776
+ }
2777
+ equals(other) {
2778
+ return other instanceof _EnumVariant && other.enum === this.enum && other.variant === this.variant;
2779
+ }
2780
+ toJSON() {
2781
+ return this.variant;
2782
+ }
2783
+ encode() {
2784
+ return { type: this.type, value: this.variant };
2785
+ }
2786
+ };
2787
+
2751
2788
  // src/value/uuid4.ts
2752
2789
  import { v4 as uuidv4, NIL as NIL_UUID, validate, version } from "uuid";
2753
2790
  var Uuid4Value = class _Uuid4Value {
@@ -3256,6 +3293,9 @@ function typeToWire(type) {
3256
3293
  if (isRecordType(type)) {
3257
3294
  return { id: "Record", underlying: type.Record.map((field) => ({ name: field.name, type: typeToWire(field.type) })) };
3258
3295
  }
3296
+ if (isEnumType(type)) {
3297
+ return { id: "Enum", enum: type.Enum };
3298
+ }
3259
3299
  return { id: type };
3260
3300
  }
3261
3301
  function typeFromWire(wire) {
@@ -3275,6 +3315,12 @@ function typeFromWire(wire) {
3275
3315
  }
3276
3316
  return digestType(inner, wire.accuracy);
3277
3317
  }
3318
+ if (wire.id === "Enum") {
3319
+ if (typeof wire.enum !== "string") {
3320
+ throw new Error(`Enum type descriptor needs a string enum, got ${JSON.stringify(wire)}`);
3321
+ }
3322
+ return { Enum: wire.enum };
3323
+ }
3278
3324
  if (wire.id === "Decimal") {
3279
3325
  const precision = wire.precision ?? FIXED_POINT_MAX_PRECISION;
3280
3326
  return decimalType(precision, wire.scale ?? DECIMAL_DEFAULT_SCALE);
@@ -3365,6 +3411,9 @@ function payloadOf(row, name, type) {
3365
3411
  }
3366
3412
 
3367
3413
  // src/value/index.ts
3414
+ function isEnumType(t) {
3415
+ return typeof t === "object" && t !== null && "Enum" in t;
3416
+ }
3368
3417
  function isOptionType(t) {
3369
3418
  return typeof t === "object" && t !== null && "Option" in t;
3370
3419
  }
@@ -3406,6 +3455,12 @@ function decode(pair) {
3406
3455
  }
3407
3456
  return new NoneValue(stripOptionLayers(pair.type, noneAt + 1));
3408
3457
  }
3458
+ if (isEnumType(pair.type)) {
3459
+ if (typeof pair.value !== "string") {
3460
+ throw new Error(`Enum cell must be a JSON string, got ${typeof pair.value}`);
3461
+ }
3462
+ return new EnumVariant(pair.type.Enum, pair.value);
3463
+ }
3409
3464
  if (isDigestType(pair.type)) {
3410
3465
  return DigestValue.parse(pair.value, pair.type);
3411
3466
  }
@@ -3843,6 +3898,171 @@ var ShapeBuilder = class {
3843
3898
  };
3844
3899
  var Shape = ShapeBuilder;
3845
3900
 
3901
+ // src/shape/widen.ts
3902
+ var NUMERIC = {
3903
+ Int1: { kind: "int", bits: 8 },
3904
+ Int2: { kind: "int", bits: 16 },
3905
+ Int4: { kind: "int", bits: 32 },
3906
+ Int8: { kind: "int", bits: 64 },
3907
+ Int16: { kind: "int", bits: 128 },
3908
+ Uint1: { kind: "uint", bits: 8 },
3909
+ Uint2: { kind: "uint", bits: 16 },
3910
+ Uint4: { kind: "uint", bits: 32 },
3911
+ Uint8: { kind: "uint", bits: 64 },
3912
+ Uint16: { kind: "uint", bits: 128 },
3913
+ Float4: { kind: "float", bits: 32 },
3914
+ Float8: { kind: "float", bits: 64 }
3915
+ };
3916
+ function widens(target, actual) {
3917
+ if (typeof actual !== "string") {
3918
+ return false;
3919
+ }
3920
+ const t = NUMERIC[target];
3921
+ const a = NUMERIC[actual];
3922
+ if (!t || !a) {
3923
+ return target === actual;
3924
+ }
3925
+ if (t.kind === "float" || a.kind === "float") {
3926
+ return t.kind === "float" && a.kind === "float" && t.bits >= a.bits;
3927
+ }
3928
+ if (t.kind === "uint") {
3929
+ return a.kind === "uint" && t.bits >= a.bits;
3930
+ }
3931
+ return a.kind === "int" ? t.bits >= a.bits : t.bits > a.bits;
3932
+ }
3933
+ function widen(value, target) {
3934
+ if (!value || typeof value !== "object" || typeof value.type !== "string") {
3935
+ return value;
3936
+ }
3937
+ const actual = value.type;
3938
+ if (actual === target || !widens(target, actual)) {
3939
+ return value;
3940
+ }
3941
+ const inner = value.value;
3942
+ if (inner === void 0 || inner === null) {
3943
+ return value;
3944
+ }
3945
+ return decode({ type: target, value: String(inner) });
3946
+ }
3947
+
3948
+ // src/shape/check.ts
3949
+ var ShapeMismatch = class extends Error {
3950
+ constructor(message) {
3951
+ super(message);
3952
+ this.name = "ShapeMismatch";
3953
+ Object.setPrototypeOf(this, new.target.prototype);
3954
+ }
3955
+ };
3956
+ function checkFrames(frames, shapes) {
3957
+ if (shapes.length === 0) {
3958
+ return;
3959
+ }
3960
+ if (frames.length !== shapes.length) {
3961
+ throw new ShapeMismatch(`expected ${shapes.length} frames for ${shapes.length} shapes, got ${frames.length}`);
3962
+ }
3963
+ frames.forEach((frame, i) => checkFrame(frame.columns, shapes[i]));
3964
+ }
3965
+ function checkFrame(columns, shape) {
3966
+ if (shape.kind !== "object") {
3967
+ throw new ShapeMismatch(`a frame needs an object shape, got ${shape.kind}`);
3968
+ }
3969
+ for (const [name, propertyShape] of Object.entries(shape.properties)) {
3970
+ const column = columns.find((c) => c.name === name);
3971
+ if (!column) {
3972
+ throw new ShapeMismatch(`column "${name}": expected ${describe(propertyShape)}, missing from frame`);
3973
+ }
3974
+ if (!matches(propertyShape, column.type)) {
3975
+ throw new ShapeMismatch(`column "${name}": expected ${describe(propertyShape)}, got ${typeName2(column.type)}`);
3976
+ }
3977
+ }
3978
+ }
3979
+ var FAMILIES = [
3980
+ ["Boolean"],
3981
+ ["Int1", "Int2", "Int4", "Int8", "Int16", "Uint1", "Uint2", "Uint4", "Uint8", "Uint16", "Float4", "Float8"],
3982
+ ["Utf8", "Decimal", "Duration", "Uuid4", "Uuid7", "IdentityId"],
3983
+ ["Date", "DateTime", "Time"],
3984
+ ["Blob"],
3985
+ ["None"]
3986
+ ];
3987
+ function familyOf(type) {
3988
+ return FAMILIES.find((family) => family.includes(type)) ?? [type];
3989
+ }
3990
+ function matches(shape, type) {
3991
+ if (isEnumType(type)) {
3992
+ return matches(shape, "Utf8");
3993
+ }
3994
+ if (isFixedPointType(type)) {
3995
+ const kind = fixedPointKind(type);
3996
+ if (shape.kind === "primitive") return familyOf(shape.type).includes(kind);
3997
+ if (shape.kind === "value") return shape.type === kind;
3998
+ return false;
3999
+ }
4000
+ switch (shape.kind) {
4001
+ case "primitive":
4002
+ if (shape.type === "None") {
4003
+ return type === "None" || isOptionType(type);
4004
+ }
4005
+ return typeof type === "string" && familyOf(shape.type).includes(type);
4006
+ case "value":
4007
+ if (shape.type === "None") {
4008
+ return type === "None" || isOptionType(type);
4009
+ }
4010
+ return type === shape.type || widens(shape.type, type);
4011
+ case "option":
4012
+ return isOptionType(type) ? matches(shape.inner, type.Option) : matches(shape.inner, type);
4013
+ case "object":
4014
+ case "array":
4015
+ return false;
4016
+ }
4017
+ }
4018
+ function describe(shape) {
4019
+ switch (shape.kind) {
4020
+ case "primitive":
4021
+ case "value":
4022
+ return shape.type;
4023
+ case "option":
4024
+ return `Option(${describe(shape.inner)})`;
4025
+ case "object":
4026
+ case "array":
4027
+ return shape.kind;
4028
+ }
4029
+ }
4030
+ function typeName2(type) {
4031
+ if (isDigestType(type)) {
4032
+ return digestTypeName(type);
4033
+ }
4034
+ if (isFixedPointType(type)) {
4035
+ return fixedPointTypeName(type);
4036
+ }
4037
+ if (isOptionType(type)) {
4038
+ return `Option(${typeName2(type.Option)})`;
4039
+ }
4040
+ if (isListType(type)) {
4041
+ return `List(${typeName2(type.List)})`;
4042
+ }
4043
+ if (isRecordType(type)) {
4044
+ return `Record(${type.Record.map((field) => `${field.name}: ${typeName2(field.type)}`).join(", ")})`;
4045
+ }
4046
+ if (isEnumType(type)) {
4047
+ return `Enum(${type.Enum})`;
4048
+ }
4049
+ return type;
4050
+ }
4051
+
4052
+ // src/shape/type.ts
4053
+ function shapeType(shape) {
4054
+ switch (shape.kind) {
4055
+ case "primitive":
4056
+ case "value":
4057
+ return shape.type;
4058
+ case "option":
4059
+ return { Option: shapeType(shape.inner) };
4060
+ case "object":
4061
+ case "array":
4062
+ throw new ShapeMismatch(`a ${shape.kind} shape has no wire type`);
4063
+ }
4064
+ }
4065
+
3846
4066
  // src/shape/parser.ts
3847
4067
  function createValueInstance(type, value) {
3848
4068
  switch (type) {
@@ -3923,7 +4143,7 @@ function parseValue(shape, value) {
3923
4143
  }
3924
4144
  if (shape.kind === "option") {
3925
4145
  if (value === null || value === void 0) {
3926
- return void 0;
4146
+ return Option.none(shapeType(shape.inner));
3927
4147
  }
3928
4148
  if (isOption(value)) {
3929
4149
  return value.map((item) => parseValue(shape.inner, item));
@@ -4016,151 +4236,6 @@ function validateShape(shape, value) {
4016
4236
  return false;
4017
4237
  }
4018
4238
 
4019
- // src/shape/widen.ts
4020
- var NUMERIC = {
4021
- Int1: { kind: "int", bits: 8 },
4022
- Int2: { kind: "int", bits: 16 },
4023
- Int4: { kind: "int", bits: 32 },
4024
- Int8: { kind: "int", bits: 64 },
4025
- Int16: { kind: "int", bits: 128 },
4026
- Uint1: { kind: "uint", bits: 8 },
4027
- Uint2: { kind: "uint", bits: 16 },
4028
- Uint4: { kind: "uint", bits: 32 },
4029
- Uint8: { kind: "uint", bits: 64 },
4030
- Uint16: { kind: "uint", bits: 128 },
4031
- Float4: { kind: "float", bits: 32 },
4032
- Float8: { kind: "float", bits: 64 }
4033
- };
4034
- function widens(target, actual) {
4035
- if (typeof actual !== "string") {
4036
- return false;
4037
- }
4038
- const t = NUMERIC[target];
4039
- const a = NUMERIC[actual];
4040
- if (!t || !a) {
4041
- return target === actual;
4042
- }
4043
- if (t.kind === "float" || a.kind === "float") {
4044
- return t.kind === "float" && a.kind === "float" && t.bits >= a.bits;
4045
- }
4046
- if (t.kind === "uint") {
4047
- return a.kind === "uint" && t.bits >= a.bits;
4048
- }
4049
- return a.kind === "int" ? t.bits >= a.bits : t.bits > a.bits;
4050
- }
4051
- function widen(value, target) {
4052
- if (!value || typeof value !== "object" || typeof value.type !== "string") {
4053
- return value;
4054
- }
4055
- const actual = value.type;
4056
- if (actual === target || !widens(target, actual)) {
4057
- return value;
4058
- }
4059
- const inner = value.value;
4060
- if (inner === void 0 || inner === null) {
4061
- return value;
4062
- }
4063
- return decode({ type: target, value: String(inner) });
4064
- }
4065
-
4066
- // src/shape/check.ts
4067
- var ShapeMismatch = class extends Error {
4068
- constructor(message) {
4069
- super(message);
4070
- this.name = "ShapeMismatch";
4071
- Object.setPrototypeOf(this, new.target.prototype);
4072
- }
4073
- };
4074
- function checkFrames(frames, shapes) {
4075
- if (shapes.length === 0) {
4076
- return;
4077
- }
4078
- if (frames.length !== shapes.length) {
4079
- throw new ShapeMismatch(`expected ${shapes.length} frames for ${shapes.length} shapes, got ${frames.length}`);
4080
- }
4081
- frames.forEach((frame, i) => checkFrame(frame.columns, shapes[i]));
4082
- }
4083
- function checkFrame(columns, shape) {
4084
- if (shape.kind !== "object") {
4085
- throw new ShapeMismatch(`a frame needs an object shape, got ${shape.kind}`);
4086
- }
4087
- for (const [name, propertyShape] of Object.entries(shape.properties)) {
4088
- const column = columns.find((c) => c.name === name);
4089
- if (!column) {
4090
- throw new ShapeMismatch(`column "${name}": expected ${describe(propertyShape)}, missing from frame`);
4091
- }
4092
- if (!matches(propertyShape, column.type)) {
4093
- throw new ShapeMismatch(`column "${name}": expected ${describe(propertyShape)}, got ${typeName2(column.type)}`);
4094
- }
4095
- }
4096
- }
4097
- var FAMILIES = [
4098
- ["Boolean"],
4099
- ["Int1", "Int2", "Int4", "Int8", "Int16", "Uint1", "Uint2", "Uint4", "Uint8", "Uint16", "Float4", "Float8"],
4100
- ["Utf8", "Decimal", "Duration", "Uuid4", "Uuid7", "IdentityId"],
4101
- ["Date", "DateTime", "Time"],
4102
- ["Blob"],
4103
- ["None"]
4104
- ];
4105
- function familyOf(type) {
4106
- return FAMILIES.find((family) => family.includes(type)) ?? [type];
4107
- }
4108
- function matches(shape, type) {
4109
- if (isFixedPointType(type)) {
4110
- const kind = fixedPointKind(type);
4111
- if (shape.kind === "primitive") return familyOf(shape.type).includes(kind);
4112
- if (shape.kind === "value") return shape.type === kind;
4113
- return false;
4114
- }
4115
- switch (shape.kind) {
4116
- case "primitive":
4117
- if (shape.type === "None") {
4118
- return type === "None" || isOptionType(type);
4119
- }
4120
- return typeof type === "string" && familyOf(shape.type).includes(type);
4121
- case "value":
4122
- if (shape.type === "None") {
4123
- return type === "None" || isOptionType(type);
4124
- }
4125
- return type === shape.type || widens(shape.type, type);
4126
- case "option":
4127
- return isOptionType(type) && matches(shape.inner, type.Option);
4128
- case "object":
4129
- case "array":
4130
- return false;
4131
- }
4132
- }
4133
- function describe(shape) {
4134
- switch (shape.kind) {
4135
- case "primitive":
4136
- case "value":
4137
- return shape.type;
4138
- case "option":
4139
- return `Option(${describe(shape.inner)})`;
4140
- case "object":
4141
- case "array":
4142
- return shape.kind;
4143
- }
4144
- }
4145
- function typeName2(type) {
4146
- if (isDigestType(type)) {
4147
- return digestTypeName(type);
4148
- }
4149
- if (isFixedPointType(type)) {
4150
- return fixedPointTypeName(type);
4151
- }
4152
- if (isOptionType(type)) {
4153
- return `Option(${typeName2(type.Option)})`;
4154
- }
4155
- if (isListType(type)) {
4156
- return `List(${typeName2(type.List)})`;
4157
- }
4158
- if (isRecordType(type)) {
4159
- return `Record(${type.Record.map((field) => `${field.name}: ${typeName2(field.type)}`).join(", ")})`;
4160
- }
4161
- return type;
4162
- }
4163
-
4164
4239
  // src/shape/transform.ts
4165
4240
  function snakeToCamel(key) {
4166
4241
  return key.replace(/_([a-z0-9])/g, (_, c) => c.toUpperCase());
@@ -4182,12 +4257,7 @@ function transformResult(row, resultShape) {
4182
4257
  const propertyShape = resultShape.properties[key];
4183
4258
  const outputKey = snakeToCamel(key);
4184
4259
  if (propertyShape && propertyShape.kind === "primitive") {
4185
- if (value && typeof value === "object" && typeof value.valueOf === "function") {
4186
- const rawValue = value.valueOf();
4187
- transformedRow[outputKey] = coerceToPrimitiveType(rawValue, propertyShape.type);
4188
- } else {
4189
- transformedRow[outputKey] = coerceToPrimitiveType(value, propertyShape.type);
4190
- }
4260
+ transformedRow[outputKey] = toPrimitive(value, propertyShape.type);
4191
4261
  } else if (propertyShape && propertyShape.kind === "value") {
4192
4262
  transformedRow[outputKey] = widen(value, propertyShape.type);
4193
4263
  } else {
@@ -4197,10 +4267,7 @@ function transformResult(row, resultShape) {
4197
4267
  return transformedRow;
4198
4268
  }
4199
4269
  if (resultShape && resultShape.kind === "primitive") {
4200
- if (row && typeof row === "object" && typeof row.valueOf === "function") {
4201
- return coerceToPrimitiveType(row.valueOf(), resultShape.type);
4202
- }
4203
- return coerceToPrimitiveType(row, resultShape.type);
4270
+ return toPrimitive(row, resultShape.type);
4204
4271
  }
4205
4272
  if (resultShape && resultShape.kind === "value") {
4206
4273
  return widen(row, resultShape.type);
@@ -4212,6 +4279,9 @@ function transformResult(row, resultShape) {
4212
4279
  return row;
4213
4280
  }
4214
4281
  if (resultShape && resultShape.kind === "option") {
4282
+ if (row === null || row === void 0) {
4283
+ return Option.none(shapeType(resultShape.inner));
4284
+ }
4215
4285
  if (row instanceof NoneValue) {
4216
4286
  const remaining = optionLayers(resultShape);
4217
4287
  const depth = noneDepth(row);
@@ -4229,6 +4299,15 @@ function transformResult(row, resultShape) {
4229
4299
  function optionLayers(shape) {
4230
4300
  return shape.kind === "option" ? optionLayers(shape.inner) + 1 : 0;
4231
4301
  }
4302
+ function toPrimitive(value, valueType) {
4303
+ if (value instanceof DurationValue || value instanceof TimeValue) {
4304
+ return value.toString();
4305
+ }
4306
+ if (value && typeof value === "object" && typeof value.valueOf === "function") {
4307
+ return coerceToPrimitiveType(value.valueOf(), valueType);
4308
+ }
4309
+ return coerceToPrimitiveType(value, valueType);
4310
+ }
4232
4311
  function coerceToPrimitiveType(value, valueType) {
4233
4312
  if (value === void 0 || value === null) {
4234
4313
  return value;
@@ -4798,6 +4877,7 @@ var ReifyError = class extends Error {
4798
4877
  this.help = diagnostic.help;
4799
4878
  this.notes = diagnostic.notes ?? [];
4800
4879
  this.cause = diagnostic.cause;
4880
+ this.index = response.payload.index;
4801
4881
  Object.setPrototypeOf(this, new.target.prototype);
4802
4882
  }
4803
4883
  toString() {
@@ -4823,6 +4903,7 @@ export {
4823
4903
  DecimalValue,
4824
4904
  DigestValue,
4825
4905
  DurationValue,
4906
+ EnumVariant,
4826
4907
  FIXED_POINT_MAX_PRECISION,
4827
4908
  FIXED_POINT_NARROW_PRECISION,
4828
4909
  Float4Value,
@@ -4886,6 +4967,7 @@ export {
4886
4967
  inferColumns,
4887
4968
  innerOfOption,
4888
4969
  isDigestType,
4970
+ isEnumType,
4889
4971
  isFixedPointType,
4890
4972
  isListType,
4891
4973
  isOption,