ballerina-core 1.0.26 → 1.0.28

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/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "ballerina-core",
3
3
  "author": "Dr. Giuseppe Maggiore",
4
4
  "private": false,
5
- "version": "1.0.26",
5
+ "version": "1.0.28",
6
6
  "main": "main.ts",
7
7
  "dependencies": {
8
8
  "immutable": "^5.0.0-beta.5",
@@ -71,8 +71,9 @@ export const editFormRunner = <E, FS>() => {
71
71
  Co.Template<EditFormForeignMutationsExpected<E, FS>>(synchronize, {
72
72
  interval: 100,
73
73
  runFilter: (props) =>
74
- Debounced.Operations.shouldCoroutineRun(props.context.apiRunner) ||
75
- !ApiResponseChecker.Operations.checked(props.context),
74
+ props.context.entity.sync.kind == "loaded" &&
75
+ (Debounced.Operations.shouldCoroutineRun(props.context.apiRunner) ||
76
+ !ApiResponseChecker.Operations.checked(props.context)),
76
77
  }),
77
78
  ]);
78
79
  };
@@ -27,7 +27,7 @@ export const EditFormState = <E,FS>() => ({
27
27
  Synchronized.Default(unit)
28
28
  ),
29
29
  formState:initialFormState,
30
- ...ApiResponseChecker.Default(false),
30
+ ...ApiResponseChecker.Default(true),
31
31
  }),
32
32
  Updaters:{
33
33
  Core:{
@@ -1,6 +1,6 @@
1
1
  import { AsyncState, builtInsFromFieldViews, FormsConfig, Sum, Synchronize, Unit } from "../../../../../main"
2
2
  import { CoTypedFactory } from "../../../../coroutines/builder"
3
- import { FormParsingResult, FormsParserContext, FormsParserState, parseForms } from "../state"
3
+ import { FormParsingResult, FormsParserContext, FormsParserState, parseForms, replaceKeywords } from "../state"
4
4
 
5
5
  export const LoadValidateAndParseFormsConfig = () => {
6
6
  const Co = CoTypedFactory<FormsParserContext, FormsParserState>()
@@ -8,7 +8,8 @@ export const LoadValidateAndParseFormsConfig = () => {
8
8
  return Co.Template<Unit>(
9
9
  Co.GetState().then(current =>
10
10
  Synchronize<Unit, FormParsingResult>(async() => {
11
- const formsConfig = await current.getFormsConfig()
11
+ const rawFormsConfig = await current.getFormsConfig();
12
+ const formsConfig = replaceKeywords(rawFormsConfig, "from api")
12
13
  const builtIns = builtInsFromFieldViews(current.fieldViews, current.fieldTypeConverters)
13
14
  const validationResult = FormsConfig.Default.validateAndParseAPIResponse(builtIns)(formsConfig)
14
15
  if (validationResult.kind == "r")
@@ -2,8 +2,7 @@ import { Map, List, Set, OrderedMap } from "immutable"
2
2
  import { CollectionReference } from "../../../collection/domains/reference/state";
3
3
  import { CollectionSelection } from "../../../collection/domains/selection/state";
4
4
  import { BasicFun } from "../../../../../fun/state";
5
- import { Option, Sum } from "../../../../../collections/domains/sum/state"
6
- import { Type, TypeDefinition, TypeName } from "../../../../../../main";
5
+ import { replaceKeyword, replaceKeywords, revertKeyword, Type, TypeDefinition, TypeName } from "../../../../../../main";
7
6
 
8
7
  export const PrimitiveTypes =
9
8
  ["string",
@@ -117,86 +116,93 @@ export const defaultValue = (types:Map<TypeName, TypeDefinition>, builtIns:Built
117
116
  }
118
117
  }
119
118
 
120
- export const fromAPIRawValue = (t:Type, types:Map<TypeName, TypeDefinition>, builtIns:BuiltIns, converters:ApiConverters) => (raw:any) : any => {
119
+ export const fromAPIRawValue = (t:Type, types:Map<TypeName, TypeDefinition>, builtIns:BuiltIns, converters:ApiConverters, isKeywordsReplaced: boolean = false) => (raw:any) : any => {
121
120
  // alert(JSON.stringify(t))
122
121
  if (raw == undefined) {
123
122
  console.warn(`instantiating default value for type ${JSON.stringify(t)}: the value was undefined so something is missing from the API response`)
124
123
  return defaultValue(types, builtIns)(t.kind == "primitive" ? t.value : t.kind == "lookup" ? t.name : t.value)
125
124
  }
126
125
 
126
+ const obj = !isKeywordsReplaced ? replaceKeywords(raw, "from api") : raw
127
+
127
128
  if (t.kind == "primitive") {
128
- return converters[t.value].fromAPIRawValue(raw)
129
+ return converters[t.value].fromAPIRawValue(obj)
129
130
  } else if (t.kind == "application") { // application here means "generic type application"
130
131
  if (t.value == "SingleSelection" && t.args.length == 1) {
131
- let result = converters[t.value].fromAPIRawValue(raw)
132
+ let result = converters[t.value].fromAPIRawValue(obj)
132
133
  result = CollectionSelection().Updaters.left(
133
- fromAPIRawValue({ kind:"lookup", name:t.args[0] }, types, builtIns, converters))(result)
134
+ fromAPIRawValue({ kind:"lookup", name:t.args[0] }, types, builtIns, converters, true))(result)
134
135
  return result
135
136
  }
136
137
  if ((t.value == "Multiselection" || t.value == "MultiSelection") && t.args.length == 1) {
137
- let result = converters["MultiSelection"].fromAPIRawValue(raw)
138
- result = result.map(fromAPIRawValue({ kind:"lookup", name:t.args[0] }, types, builtIns, converters))
138
+ let result = converters["MultiSelection"].fromAPIRawValue(obj)
139
+ result = result.map(fromAPIRawValue({ kind:"lookup", name:t.args[0] }, types, builtIns, converters, true))
139
140
  return result
140
141
  }
141
142
  if (t.value == "List" && t.args.length == 1) {
142
- let result = converters[t.value].fromAPIRawValue(raw)
143
+ let result = converters[t.value].fromAPIRawValue(obj)
143
144
  result = result.map(fromAPIRawValue(
144
145
  PrimitiveTypes.some(_ => _ == t.args[0]) ?
145
146
  { kind:"primitive", value:t.args[0] as PrimitiveType }
146
147
  : { kind:"lookup", name:t.args[0] }
147
- , types, builtIns, converters))
148
+ , types, builtIns, converters, true))
148
149
  return result
149
150
  }
150
151
  } else { // t.kind == lookup: we are dealing with a record/object
151
- let result:any = {...raw}
152
+ let result:any = {...obj}
152
153
  const tDef = types.get(t.name)!
153
154
  tDef.fields.forEach((fieldType, fieldName) => {
154
- const fieldValue = raw[fieldName]
155
- result[fieldName] = fromAPIRawValue(fieldType, types, builtIns, converters)(fieldValue)
155
+ const replacedFieldName = replaceKeyword(fieldName)
156
+ const fieldValue = obj[replacedFieldName]
157
+ result[replacedFieldName] = fromAPIRawValue(fieldType, types, builtIns, converters, true)(fieldValue)
156
158
  })
157
159
  return result
158
160
  }
159
- console.error(`unsupported type ${JSON.stringify(t)}, returning the raw value right away`)
160
- return raw
161
+ console.error(`unsupported type ${JSON.stringify(t)}, returning the obj value right away`)
162
+ return obj
161
163
  }
162
164
 
163
165
 
164
- export const toAPIRawValue = (t:Type, types:Map<TypeName, TypeDefinition>, builtIns:BuiltIns, converters:ApiConverters) => (raw:any) : any => {
166
+ export const toAPIRawValue = (t:Type, types:Map<TypeName, TypeDefinition>, builtIns:BuiltIns, converters:ApiConverters, isKeywordsReverted: boolean = false) => (raw:any) : any => {
167
+
168
+ const obj = !isKeywordsReverted ? replaceKeywords(raw, "to api") : raw
169
+
165
170
  if (t.kind == "primitive") {
166
- return converters[t.value].toAPIRawValue(raw as never)
171
+ return converters[t.value].toAPIRawValue(obj as never)
167
172
  } else if (t.kind == "application") { // application here means "generic type application"
168
173
  if (t.value == "SingleSelection" && t.args.length == 1) {
169
- let result = converters[t.value].toAPIRawValue(raw)
174
+ let result = converters[t.value].toAPIRawValue(obj)
170
175
  if (result != undefined && typeof result == "object")
171
- result = toAPIRawValue({ kind:"lookup", name:t.args[0] }, types, builtIns, converters)(result)
176
+ result = toAPIRawValue({ kind:"lookup", name:t.args[0] }, types, builtIns, converters, true)(result)
172
177
  return result
173
178
  }
174
179
  if ((t.value == "Multiselection" || t.value == "MultiSelection") && t.args.length == 1) {
175
- // alert(`MultiSelect ${JSON.stringify(t)} ${JSON.stringify(raw)}`)
176
- let result = converters["MultiSelection"].toAPIRawValue(raw)
180
+ // alert(`MultiSelect ${JSON.stringify(t)} ${JSON.stringify(obj)}`)
181
+ let result = converters["MultiSelection"].toAPIRawValue(obj)
177
182
  // alert(`MultiSelect result1 = ${JSON.stringify(result)}`)
178
183
  // alert(`${JSON.stringify(t.args[0])}`)
179
184
  result = result.map((_:any) =>
180
- typeof _ == "object" ? toAPIRawValue({ kind:"lookup", name:t.args[0] }, types, builtIns, converters)(_) : _)
185
+ typeof _ == "object" ? toAPIRawValue({ kind:"lookup", name:t.args[0] }, types, builtIns, converters, true)(_) : _)
181
186
  // alert(`MultiSelect result2 = ${JSON.stringify(result)}`)
182
187
  return result
183
188
  }
184
189
  if (t.value == "List" && t.args.length == 1) {
185
- let result = converters[t.value].toAPIRawValue(raw)
190
+ let result = converters[t.value].toAPIRawValue(obj)
186
191
  result = result.map(toAPIRawValue(
187
192
  PrimitiveTypes.some(_ => _ == t.args[0]) ?
188
193
  { kind:"primitive", value:t.args[0] as PrimitiveType }
189
194
  : { kind:"lookup", name:t.args[0] },
190
195
  // { kind:"lookup", name:t.args[0] },
191
- types, builtIns, converters))
196
+ types, builtIns, converters, true))
192
197
  return result
193
198
  }
194
199
  } else { // t.kind == lookup: we are dealing with a record/object
195
- let result:any = {...raw}
200
+ let result:any = {...obj}
196
201
  const tDef = types.get(t.name)!
197
202
  tDef.fields.forEach((fieldType, fieldName) => {
198
- const fieldValue = raw[fieldName]
199
- result[fieldName] = toAPIRawValue(fieldType, types, builtIns, converters)(fieldValue)
203
+ const revertedFieldName = revertKeyword(fieldName)
204
+ const fieldValue = obj[revertedFieldName]
205
+ result[revertedFieldName] = toAPIRawValue(fieldType, types, builtIns, converters, true)(fieldValue)
200
206
  })
201
207
  return result
202
208
  }
@@ -1,4 +1,4 @@
1
- import { Collection, Map, OrderedMap, OrderedSet, Set } from "immutable";
1
+ import { List, Map, OrderedMap, OrderedSet, Set } from "immutable";
2
2
  import { BoolExpr, Unit, PromiseRepo, Guid, LeafPredicatesEvaluators, Predicate, FormsConfig, BuiltIns, FormDef, Sum, BasicFun, Template, unit, EditFormState, EditFormTemplate, ApiErrors, CreateFormTemplate, EntityFormTemplate, SharedFormState, CreateFormState, Entity, EditFormContext, CreateFormContext, MappedEntityFormTemplate, Mapping, FormValidationResult, Synchronized, simpleUpdater, PrimitiveType, GenericType, ApiConverter, TypeName, ListFieldState, ListForm, TypeDefinition, ApiConverters, defaultValue, fromAPIRawValue, toAPIRawValue, EditFormForeignMutationsExpected } from "../../../../main";
3
3
  import { Value } from "../../../value/state";
4
4
  import { CollectionReference } from "../collection/domains/reference/state";
@@ -385,7 +385,8 @@ export const parseForms =
385
385
  const parsed = fromAPIRawValue({ kind: "lookup", name: parsedForm.formDef.type }, formsConfig.types, builtIns, apiConverters)(raw)
386
386
  return parsed
387
387
  }),
388
- update: (value: any) => entityApis.update(launcher.api)(toAPIRawValue({ kind: "lookup", name: parsedForm.formDef.type }, formsConfig.types, builtIns, apiConverters)(value))
388
+ update: (value: any) =>
389
+ entityApis.update(launcher.api)(toAPIRawValue({ kind: "lookup", name: parsedForm.formDef.type }, formsConfig.types, builtIns, apiConverters)(value))
389
390
  }
390
391
  parsedLaunchers.edit = parsedLaunchers.edit.set(
391
392
  launcherName,
@@ -468,7 +469,60 @@ export const parseForms =
468
469
  return Sum.Default.left(parsedLaunchers)
469
470
  }
470
471
 
472
+ const OVERRIDE_FIELDS = ["value", "sync"];
471
473
 
474
+ export const replaceKeyword = (fieldName: string): string => {
475
+ if (OVERRIDE_FIELDS.includes(fieldName)) {
476
+ return `__keywordreplacement__${fieldName}__`;
477
+ }
478
+ return fieldName;
479
+ };
480
+
481
+ export const revertKeyword = (fieldName: string): string => {
482
+ const overridenFieldNames = OVERRIDE_FIELDS.reduce((acc, field) => {
483
+ const overrideName = `__keywordreplacement__${field}__`;
484
+ acc[overrideName] = field;
485
+ return acc;
486
+ }, {} as any);
487
+
488
+ if (fieldName in overridenFieldNames) {
489
+ return overridenFieldNames[fieldName];
490
+ }
491
+ return fieldName;
492
+ };
493
+
494
+ export const replaceKeywords = (obj: any, kind: "from api" | "to api"): any => {
495
+ const replacementFn = kind == "from api" ? replaceKeyword : revertKeyword;
496
+ if (Array.isArray(obj) || List.isList(obj)) {
497
+ return obj.map((item) =>
498
+ typeof item == "string"
499
+ ? replacementFn(item)
500
+ : replaceKeywords(item, kind )
501
+ );
502
+ } else if (typeof obj === "object" && obj !== null) {
503
+ if(OrderedMap.isOrderedMap(obj)) {
504
+ return obj.map((_, key) =>
505
+ replacementFn(key as string)
506
+ )
507
+ }
508
+ if(Map.isMap(obj)) {
509
+ return obj.map((_, key) =>
510
+ replacementFn(key as string)
511
+ )
512
+ }
513
+ const copy = { ...obj };
514
+ Object.keys(copy).forEach((key) => {
515
+ const value = copy[key];
516
+ const newKeyName = replacementFn(key);
517
+ copy[newKeyName] = replaceKeywords(value, kind);
518
+ if(newKeyName !== key) {
519
+ delete copy[key]
520
+ }
521
+ });
522
+ return copy;
523
+ }
524
+ return obj;
525
+ };
472
526
 
473
527
  export type FormsParserContext = {
474
528
  containerFormView: any,