Cole-Greer commented on code in PR #3309: URL: https://github.com/apache/tinkerpop/pull/3309#discussion_r2876127002
########## gremlin-dotnet/src/Gremlin.Net/Process/Traversal/GremlinLang.cs: ########## @@ -0,0 +1,750 @@ +#region License + +/* + * 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. + */ + +#endregion + +using System; +using System.Collections; +using System.Collections.Generic; +using System.Globalization; +using System.Numerics; +using System.Text; +using System.Threading; +using Gremlin.Net.Process.Traversal.Strategy; +using Gremlin.Net.Process.Traversal.Strategy.Decoration; +using Gremlin.Net.Structure; + +namespace Gremlin.Net.Process.Traversal +{ + /// <summary> + /// Builds a gremlin-lang compatible string representation of a traversal, + /// along with a map of named parameters. + /// </summary> + public class GremlinLang : ICloneable, IEquatable<GremlinLang> + { + private static readonly object[] EmptyArray = Array.Empty<object>(); + + private StringBuilder _gremlin = new(); + private Dictionary<string, object> _parameters = new(); + private static int _paramCount; + private List<OptionsStrategy> _optionsStrategies = new(); + + /// <summary> + /// Initializes a new instance of the <see cref="GremlinLang" /> class. + /// </summary> + public GremlinLang() + { + } + + /// <summary> + /// Adds a traversal source instruction to the GremlinLang. + /// </summary> + /// <param name="sourceName">The traversal source method name (e.g. withSack()).</param> + /// <param name="arguments">The traversal source method arguments.</param> + public void AddSource(string sourceName, params object?[] arguments) + { + if (sourceName == "withStrategies" && arguments.Length != 0) + { + var args = BuildStrategyArgs(arguments); + if (args.Length != 0) + { + _gremlin.Append('.').Append("withStrategies").Append('(').Append(args).Append(')'); + } + return; + } + + AddToGremlin(sourceName, arguments); + } + + /// <summary> + /// Adds a traversal step instruction to the GremlinLang. + /// </summary> + /// <param name="stepName">The traversal method name (e.g. out()).</param> + /// <param name="arguments">The traversal method arguments.</param> + public void AddStep(string stepName, params object?[] arguments) + { + AddToGremlin(stepName, arguments); + } + + /// <summary> + /// Sets the alias for the traversal source. + /// </summary> + /// <param name="g">The alias to set.</param> + public void AddG(string g) + { + _parameters["g"] = g; + } + + /// <summary> + /// Gets the gremlin-lang compatible string representation prefixed with "g". + /// </summary> + /// <returns>The gremlin-lang string.</returns> + public string GetGremlin() + { + return GetGremlin("g"); + } + + /// <summary> + /// Gets the gremlin-lang compatible string representation with the specified prefix. + /// </summary> + /// <param name="prefix">The prefix to use (e.g. "g" or "__").</param> + /// <returns>The gremlin-lang string.</returns> + public string GetGremlin(string prefix) + { + var gremlinStr = _gremlin.ToString(); + // special handling for CardinalityValueTraversal + if (gremlinStr.Length != 0 && gremlinStr[0] != '.') + { + return gremlinStr; + } + return prefix + gremlinStr; + } + + /// <summary> + /// Gets the parameters used in the traversal. + /// </summary> + public Dictionary<string, object> Parameters => _parameters; + + /// <summary> + /// Gets the list of extracted OptionsStrategy instances. + /// </summary> + public List<OptionsStrategy> OptionsStrategies => _optionsStrategies; + + /// <summary> + /// Gets a value indicating whether this GremlinLang has no content. + /// </summary> + public bool IsEmpty => _gremlin.Length == 0; + + /// <summary> + /// Resets the static parameter counter. Intended for test determinism. + /// </summary> + public static void ResetCounter() + { + Interlocked.Exchange(ref _paramCount, 0); + } + + private void AddToGremlin(string name, object?[] arguments) + { + var flattenedArguments = FlattenArguments(arguments); + + // special handling for CardinalityValueTraversal + if (name == "CardinalityValueTraversal") + { + _gremlin.Append("Cardinality.").Append(flattenedArguments[0]) + .Append('(').Append(flattenedArguments[1]).Append(')'); + return; + } + + _gremlin.Append('.').Append(name).Append('('); + + for (int i = 0; i < flattenedArguments.Length; i++) + { + if (i != 0) + { + _gremlin.Append(','); + } + _gremlin.Append(ArgAsString(flattenedArguments[i])); + } + + _gremlin.Append(')'); + } + + private string ArgAsString(object? arg) + { + if (arg == null) + return "null"; + + if (arg is string s) + return $"\"{EscapeJava(s)}\""; + + if (arg is bool b) + return b ? "true" : "false"; + + if (arg is byte byteVal) + return $"{byteVal}B"; + if (arg is short shortVal) + return $"{shortVal}S"; + if (arg is int intVal) + return intVal.ToString(CultureInfo.InvariantCulture); Review Comment: Just curious, was there an issue that requires `CultureInfo.InvariantCulture` here? -- 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]
