Skip to content

Commit

Permalink
update yaml schema compiler to process custom granularities
Browse files Browse the repository at this point in the history
  • Loading branch information
KSDaemon committed Aug 7, 2024
1 parent 4f82912 commit 8913e63
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
22 changes: 14 additions & 8 deletions packages/cubejs-schema-compiler/src/compiler/YamlCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,19 +294,25 @@ export class YamlCompiler {
return {};
}

const remapped = yamlArray.map(({ name, indexes, ...rest }) => {
const remapped = yamlArray.map(({ name, indexes, granularities, ...rest }) => {
if (!name) {
errorsReport.error(`name isn't defined for ${memberType}: ${JSON.stringify(rest)}`);
return {};
}

const res = { [name]: {} };
if (memberType === 'preAggregation' && indexes) {
indexes = this.yamlArrayToObj(indexes || [], `${memberType}.index`, errorsReport);
res[name] = { indexes, ...res[name] };
}

if (!name) {
errorsReport.error(`name isn't defined for ${memberType}: ${JSON.stringify(rest)}`);
return {};
} else if (indexes) {
return { [name]: { indexes, ...rest } };
} else {
return { [name]: rest };
if (memberType === 'dimension' && granularities) {
granularities = this.yamlArrayToObj(granularities || [], `${memberType}.granularity`, errorsReport);
res[name] = { granularities, ...res[name] };
}

res[name] = { ...res[name], ...rest };
return res;
});

return remapped.reduce((a, b) => ({ ...a, ...b }), {});
Expand Down
9 changes: 8 additions & 1 deletion packages/cubejs-schema-compiler/src/compiler/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { camelize } from 'inflection';

// It's a map where key - is a level and value - is a map of properties on this level to ignore camelization
const IGNORE_CAMELIZE = {
1: {
granularities: true,
}
};

function camelizeObjectPart(obj: unknown, camelizeKeys: boolean, level = 0): unknown {
if (!obj) {
return obj;
Expand All @@ -12,7 +19,7 @@ function camelizeObjectPart(obj: unknown, camelizeKeys: boolean, level = 0): unk
} else if (typeof obj === 'object') {
for (const key of Object.keys(obj)) {
if (!(level === 1 && key === 'meta')) {
obj[key] = camelizeObjectPart(obj[key], true, level + 1);
obj[key] = camelizeObjectPart(obj[key], !IGNORE_CAMELIZE[level]?.[key], level + 1);
}

if (camelizeKeys) {
Expand Down

0 comments on commit 8913e63

Please sign in to comment.