@malloydata/malloy-interfaces 0.0.243-dev250314154947 → 0.0.243-dev250314155434

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.
@@ -1,2 +1,4 @@
1
1
  export declare function nestUnions(obj: unknown): unknown;
2
2
  export declare function unnestUnions(obj: unknown, type: string): unknown;
3
+ export declare function convertFromThrift(obj: unknown, type: string): unknown;
4
+ export declare function convertToThrift(obj: unknown, type: string): unknown;
@@ -6,7 +6,7 @@
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.unnestUnions = exports.nestUnions = void 0;
9
+ exports.convertToThrift = exports.convertFromThrift = exports.unnestUnions = exports.nestUnions = void 0;
10
10
  const types_1 = require("./types");
11
11
  function nestUnions(obj) {
12
12
  if (obj === null) {
@@ -49,13 +49,7 @@ function unnestUnions(obj, type) {
49
49
  return obj.map(value => unnestUnions(value, type));
50
50
  }
51
51
  else {
52
- if (type === undefined) {
53
- throw new Error('Cannot unnest unions of object without a type');
54
- }
55
- const typeDefinition = types_1.MALLOY_INTERFACE_TYPES[type];
56
- if (typeDefinition === undefined) {
57
- throw new Error(`Unknown Malloy interface type ${type}`);
58
- }
52
+ const typeDefinition = getType(type);
59
53
  if (typeDefinition.type === 'union') {
60
54
  for (const kind in typeDefinition.options) {
61
55
  if (obj[kind] !== undefined) {
@@ -86,4 +80,113 @@ function unnestUnions(obj, type) {
86
80
  }
87
81
  }
88
82
  exports.unnestUnions = unnestUnions;
83
+ function convertFromThrift(obj, type) {
84
+ if (obj === null || obj === undefined) {
85
+ return obj;
86
+ }
87
+ else if (typeof obj === 'string') {
88
+ return obj;
89
+ }
90
+ else if (typeof obj === 'number') {
91
+ const typeDefinition = types_1.MALLOY_INTERFACE_TYPES[type];
92
+ if (typeDefinition && typeDefinition.type === 'enum') {
93
+ return typeDefinition.values[obj - 1];
94
+ }
95
+ return obj;
96
+ }
97
+ else if (Array.isArray(obj)) {
98
+ return obj.map(value => convertFromThrift(value, type));
99
+ }
100
+ else {
101
+ const typeDefinition = getType(type);
102
+ if (typeDefinition.type === 'union') {
103
+ for (const kind in typeDefinition.options) {
104
+ if (obj[kind] !== undefined) {
105
+ const result = convertFromThrift(obj[kind], typeDefinition.options[kind]);
106
+ if (typeof result === 'object') {
107
+ return {
108
+ kind,
109
+ ...result,
110
+ };
111
+ }
112
+ }
113
+ }
114
+ }
115
+ else if (typeDefinition.type === 'struct') {
116
+ const result = {};
117
+ for (const key in obj) {
118
+ const childType = typeDefinition.fields[key];
119
+ if (childType === undefined) {
120
+ throw new Error(`Unknown field ${key} in ${type}`);
121
+ }
122
+ result[key] = convertFromThrift(obj[key], childType.type);
123
+ }
124
+ return result;
125
+ }
126
+ else {
127
+ throw new Error(`Cannot unnest unions in an enum type ${type}`);
128
+ }
129
+ }
130
+ }
131
+ exports.convertFromThrift = convertFromThrift;
132
+ function getType(type) {
133
+ const typeDefinition = types_1.MALLOY_INTERFACE_TYPES[type];
134
+ if (typeDefinition === undefined) {
135
+ throw new Error(`Unknown Malloy interface type ${type}`);
136
+ }
137
+ return typeDefinition;
138
+ }
139
+ function convertToThrift(obj, type) {
140
+ if (obj === null || obj === undefined) {
141
+ return obj;
142
+ }
143
+ else if (typeof obj === 'number') {
144
+ return obj;
145
+ }
146
+ else if (typeof obj === 'string') {
147
+ if (type === 'string')
148
+ return obj;
149
+ const typeDefinition = getType(type);
150
+ if (typeDefinition.type === 'enum') {
151
+ return typeDefinition.values.indexOf(obj) + 1;
152
+ }
153
+ }
154
+ else if (Array.isArray(obj)) {
155
+ return obj.map(el => convertToThrift(el, type));
156
+ }
157
+ else {
158
+ const typeDefinition = getType(type);
159
+ if (typeDefinition.type === 'union') {
160
+ const kind = obj['kind'];
161
+ const unionType = typeDefinition.options[kind];
162
+ if (unionType === undefined) {
163
+ throw new Error(`${kind} is not a valid union of ${type}`);
164
+ }
165
+ const unionTypeDefinition = getType(unionType);
166
+ if (unionTypeDefinition.type !== 'struct') {
167
+ throw new Error('Union fields must be structs');
168
+ }
169
+ const result = {};
170
+ for (const key in obj) {
171
+ if (key === 'kind')
172
+ continue;
173
+ const childType = unionTypeDefinition.fields[key];
174
+ result[key] = convertToThrift(obj[key], childType.type);
175
+ }
176
+ return { [kind]: result };
177
+ }
178
+ else if (typeDefinition.type === 'struct') {
179
+ const result = {};
180
+ for (const key in obj) {
181
+ const childType = typeDefinition.fields[key];
182
+ if (childType === undefined) {
183
+ throw new Error(`Unknown field ${key} in ${type}`);
184
+ }
185
+ result[key] = convertToThrift(obj[key], childType.type);
186
+ }
187
+ return result;
188
+ }
189
+ }
190
+ }
191
+ exports.convertToThrift = convertToThrift;
89
192
  //# sourceMappingURL=nest_unions.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@malloydata/malloy-interfaces",
3
- "version": "0.0.243-dev250314154947",
3
+ "version": "0.0.243-dev250314155434",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",