yangzhang75 commented on code in PR #8304:
URL: https://github.com/apache/texera/pull/8304#discussion_r3908916633


##########
frontend/src/app/workspace/service/form-binding/form-binding.service.ts:
##########
@@ -0,0 +1,295 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import { Injectable } from "@angular/core";
+import { BehaviorSubject, Observable } from "rxjs";
+import { CustomJSONSchema7 } from "../../types/custom-json-schema.interface";
+import { OperatorPredicate } from "../../types/workflow-common.interface";
+import { FormFieldBinding, FormFieldOverride, FormBindingConfig } from 
"../../../common/type/workflow";
+import { OperatorMetadataService } from 
"../operator-metadata/operator-metadata.service";
+import { DynamicSchemaService } from 
"../dynamic-schema/dynamic-schema.service";
+import { WorkflowActionService } from 
"../workflow-graph/model/workflow-action.service";
+
+/** A binding paired with the operator field that renders it. */
+export interface ResolvedField {
+  binding: FormFieldBinding;
+  /** The operator's current value -- what the form shows and what a run uses. 
*/
+  value: unknown;
+  /** Label for the operator this input belongs to, for the author's 
reference. */
+  operatorLabel: string;
+  schema?: CustomJSONSchema7;
+  /**
+   * Set when the binding no longer points at a real property, because the 
operator
+   * was deleted on the regular canvas or the raw key was mistyped. Broken 
inputs are
+   * shown to the author with the reason and hidden from everyone else, since 
filling
+   * one in could not do anything.
+   */
+  brokenReason?: string;
+}
+
+/**
+ * Reads and writes the Form View definition. The one rule it enforces: a 
definition never
+ * affects a run -- filling an input is the same `setOperatorProperty` edit 
the canvas makes,
+ * and everything else (names, help text, ordering, instruction) is 
presentation.
+ */
+@Injectable({ providedIn: "root" })
+export class FormBindingService {
+  /** Whether the author is choosing which properties the form offers. Sticky 
(choosing spans
+   *  several operators), so it stays on until the author turns it off. */
+  private choosingSubject = new BehaviorSubject<boolean>(false);
+  public readonly choosing$: Observable<boolean> = 
this.choosingSubject.asObservable();
+
+  constructor(
+    private workflowActionService: WorkflowActionService,
+    private operatorMetadataService: OperatorMetadataService,
+    private dynamicSchemaService: DynamicSchemaService
+  ) {}
+
+  public isChoosing(): boolean {
+    return this.choosingSubject.value;
+  }
+
+  public setChoosing(choosing: boolean): void {
+    if (this.choosingSubject.value !== choosing) {
+      this.choosingSubject.next(choosing);
+    }
+  }
+
+  public getConfig(): FormBindingConfig {
+    return this.workflowActionService.getFormBinding();
+  }
+
+  /** Apply a partial edit to the definition, announcing it so it gets saved. 
*/
+  public updateConfig(patch: Partial<FormBindingConfig>): void {
+    this.workflowActionService.setFormBinding({ ...this.getConfig(), ...patch 
});
+  }
+
+  public setFields(fields: FormFieldBinding[]): void {
+    this.updateConfig({ fields });
+  }
+
+  public updateBinding(id: string, patch: Partial<FormFieldBinding>): void {
+    this.setFields(this.getConfig().fields.map(p => (p.id === id ? { ...p, 
...patch } : p)));
+  }
+
+  public removeBinding(id: string): void {
+    this.setFields(this.getConfig().fields.filter(p => p.id !== id));
+  }
+
+  /** The binding for one operator property, if it is currently exposed on the 
form. */
+  private findBinding(operatorID: string, propertyKey: string): 
FormFieldBinding | undefined {
+    return this.getConfig().fields.find(p => p.operatorID === operatorID && 
p.propertyKey === propertyKey);
+  }
+
+  /**
+   * Expose an operator property on the form. A no-op if it is already 
exposed; the display
+   * name starts from the schema's title so the author begins with a readable 
label.
+   */
+  public addBinding(operatorID: string, propertyKey: string): void {
+    if (this.findBinding(operatorID, propertyKey)) {
+      return;
+    }
+    const schema = this.propertySchema(operatorID, propertyKey);
+    this.setFields([
+      ...this.getConfig().fields,
+      {
+        id: `field-${crypto.randomUUID()}`,
+        operatorID,
+        propertyKey,
+        displayName: (schema?.title as string) || propertyKey,
+        // Deliberately not seeded from the schema's description. That text 
describes the
+        // operator to whoever wired it up -- "Multiple string key/value 
pairs" -- and
+        // appearing unbidden under a reader's input it is worse than nothing: 
it reads
+        // as guidance the author never wrote and cannot be told apart from 
guidance
+        // they did. Empty until the author has something to say.
+        helpText: undefined,
+      },
+    ]);
+  }
+
+  /**
+   * Apply an override to one field. An entry that no longer says anything is 
deleted
+   * rather than left as an empty object, so the saved definition stays a 
record of the
+   * author's decisions instead of accumulating every field they happened to 
look at.
+   */
+  public setFieldOverride(bindingId: string, path: string, patch: 
Partial<FormFieldOverride>): void {
+    const binding = this.getConfig().fields.find(p => p.id === bindingId);
+    if (!binding) {
+      return;
+    }
+    const merged: FormFieldOverride = { ...(binding.overrides?.[path] ?? {}), 
...patch };
+    if (merged.displayName !== undefined && merged.displayName.trim() === "") {
+      delete merged.displayName;
+    }
+    if (merged.hidden === false) {
+      delete merged.hidden;
+    }

Review Comment:
   Fixed. The cleanup now treats an explicit undefined (as well as false and 
empty) the same as absent, so a patch like { displayName: undefined } clears 
the key instead of leaving a hollow override that serializes to {}. Added a 
test for the undefined case, so the override map stays a record of real 
decisions.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to