Cole-Greer commented on code in PR #3304: URL: https://github.com/apache/tinkerpop/pull/3304#discussion_r2776178917
########## gremlin-javascript/src/main/javascript/gremlin-javascript/lib/process/gremlin-lang.ts: ########## @@ -0,0 +1,102 @@ +/* + * 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 { P, TextP, EnumValue } from './traversal.js'; +import { OptionsStrategy, TraversalStrategy } from './traversal-strategy.js'; + +export default class GremlinLang { + private gremlin: string = ''; + private optionsStrategies: OptionsStrategy[] = []; + + constructor(toClone?: GremlinLang) { + if (toClone) { + this.gremlin = toClone.gremlin; + this.optionsStrategies = [...toClone.optionsStrategies]; + } + } + + getOptionsStrategies(): OptionsStrategy[] { + return this.optionsStrategies; + } + + private _argAsString(arg: any): string { + if (arg === null || arg === undefined) return 'null'; + if (typeof arg === 'boolean') return arg ? 'true' : 'false'; + if (typeof arg === 'number') return String(arg); + if (typeof arg === 'string') { + const escaped = arg.replace(/\\/g, '\\\\').replace(/'/g, "\\'"); + return `'${escaped}'`; + } + if (arg instanceof P || arg instanceof TextP) { + return arg.toString(); + } + if (arg instanceof EnumValue) { + return arg.toString(); + } + if (arg instanceof TraversalStrategy && !(arg instanceof OptionsStrategy)) { + const simpleName = arg.fqcn.split('.').pop() || arg.fqcn; + const configEntries = Object.entries(arg.configuration); + if (configEntries.length === 0) { + return simpleName; + } + const configStr = configEntries.map(([k, v]) => `${k}:${this._argAsString(v)}`).join(', '); + return `new ${simpleName}(${configStr})`; + } + if (Array.isArray(arg)) { + return '[' + arg.map(a => this._argAsString(a)).join(', ') + ']'; + } + if (typeof arg === 'object' && arg !== null && arg.constructor === Object) { + const entries = Object.entries(arg); + if (entries.length === 0) return '[:]'; + return '[' + entries.map(([k, v]) => `${this._argAsString(k)}:${this._argAsString(v)}`).join(', ') + ']'; + } + return String(arg); Review Comment: There are a few missing types which will need cases here (and tests): - Date - Set - Map UUID is also missing however I think this can be ignored for now as I think it needs to be completely redone in JS for TP4. -- 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]
