Skip to content

Commit a5cc9b3

Browse files
committed
feat: add toUniquenessScope function to handle uniqueness scope conversion
1 parent d2a38c0 commit a5cc9b3

1 file changed

Lines changed: 15 additions & 3 deletions

File tree

ts/src/internal/module-builder.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {constructValue} from "@code0-tech/tucana/helpers";
2-
import {DefinitionDataType, FlowType, FlowTypeSetting, Module, ModuleConfigurationDefinition, RuntimeFlowType, RuntimeFlowTypeSetting} from "@code0-tech/tucana/shared";
2+
import {DefinitionDataType, FlowType, FlowTypeSetting, FlowTypeSetting_UniquenessScope, Module, ModuleConfigurationDefinition, RuntimeFlowType, RuntimeFlowTypeSetting} from "@code0-tech/tucana/shared";
33
import type {FunctionProps} from "../models/function.model";
44
import type {RuntimeFunctionProps} from "../models/runtime_function.model";
55
import type {ConfigurationDefinition, Translation} from "../types";
@@ -22,6 +22,18 @@ export interface ModuleBuildData {
2222
runtimeFunctions: RuntimeFunctionProps[];
2323
}
2424

25+
// The protobuf field is an enum (int32); string names like "PROJECT" would fail binary serialization.
26+
// Returns number since FlowTypeSetting and RuntimeFlowTypeSetting each declare their own (identical) enum.
27+
function toUniquenessScope(unique: FlowTypeSetting_UniquenessScope | keyof typeof FlowTypeSetting_UniquenessScope | undefined): number {
28+
if (unique == null) return FlowTypeSetting_UniquenessScope.NONE;
29+
if (typeof unique === "string") {
30+
const scope = FlowTypeSetting_UniquenessScope[unique];
31+
if (typeof scope !== "number") throw new Error(`Invalid uniqueness scope: ${JSON.stringify(unique)}`);
32+
return scope;
33+
}
34+
return unique;
35+
}
36+
2537
export function buildModule(data: ModuleBuildData): Module {
2638
return {
2739
identifier: data.identifier,
@@ -57,7 +69,7 @@ export function buildModule(data: ModuleBuildData): Module {
5769
identifier: ft.identifier,
5870
settings: (ft.settings ?? []).map(s => ({
5971
identifier: s.identifier,
60-
unique: s.unique ?? 1,
72+
unique: toUniquenessScope(s.unique),
6173
linkedDataTypeIdentifiers: s.linkedDataTypeIdentifiers ?? [],
6274
...(s.defaultValue != null ? {defaultValue: constructValue(s.defaultValue)} : {}),
6375
name: s.name ?? [],
@@ -82,7 +94,7 @@ export function buildModule(data: ModuleBuildData): Module {
8294
identifier: rft.identifier,
8395
runtimeSettings: (rft.settings ?? []).map(s => ({
8496
identifier: s.identifier,
85-
unique: s.unique ?? 1,
97+
unique: toUniquenessScope(s.unique),
8698
...(s.defaultValue != null ? {defaultValue: constructValue(s.defaultValue)} : {}),
8799
name: s.name ?? [],
88100
description: s.description ?? [],

0 commit comments

Comments
 (0)