xiazcy commented on code in PR #3292: URL: https://github.com/apache/tinkerpop/pull/3292#discussion_r2714515279
########## gremlin-go/driver/gremlinlang.go: ########## @@ -0,0 +1,578 @@ +/* +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. +*/ + +package gremlingo + +import ( + "fmt" + "go/token" + "math" + "math/big" + "reflect" + "strconv" + "strings" + "sync/atomic" + "time" + + "github.com/google/uuid" +) + +type GremlinLang struct { + emptyArray []interface{} + gremlin []string + parameters map[string]interface{} + optionsStrategies []*traversalStrategy + paramCount *atomic.Uint64 +} + +// NewGremlinLang creates a new GremlinLang to be used in traversals. +func NewGremlinLang(gl *GremlinLang) *GremlinLang { + gremlin := make([]string, 0) + parameters := make(map[string]interface{}) + optionsStrategies := make([]*traversalStrategy, 0) + paramCount := atomic.Uint64{} + if gl != nil { + gremlin = make([]string, len(gl.gremlin)) + copy(gremlin, gl.gremlin) + + parameters = make(map[string]interface{}) + for k, v := range gl.parameters { + parameters[k] = v + } + + optionsStrategies = make([]*traversalStrategy, len(gl.optionsStrategies)) + copy(optionsStrategies, gl.optionsStrategies) + + paramCount.Store(gl.paramCount.Load()) + } + + return &GremlinLang{ + gremlin: gremlin, + parameters: parameters, + optionsStrategies: optionsStrategies, + paramCount: ¶mCount, + } +} + +func (gl *GremlinLang) addToGremlin(name string, args ...interface{}) error { + flattenedArgs := gl.flattenArguments(args...) + if name == "CardinalityValueTraversal" { + str0, err := gl.argAsString(flattenedArgs[0]) + if err != nil { + return err + } + str1, err := gl.argAsString(flattenedArgs[1]) + if err != nil { + return err + } + gl.gremlin = append(gl.gremlin, str0) + gl.gremlin = append(gl.gremlin, "(") + gl.gremlin = append(gl.gremlin, str1) + gl.gremlin = append(gl.gremlin, ")") + return nil + } + + gl.gremlin = append(gl.gremlin, ".") + gl.gremlin = append(gl.gremlin, name) + gl.gremlin = append(gl.gremlin, "(") + + for i := 0; i < len(flattenedArgs); i++ { + if i > 0 { + gl.gremlin = append(gl.gremlin, ",") + } + convertArg, err := gl.convertArgument(flattenedArgs[i]) //.Index(i).Interface()) + if err != nil { + return err + } + argStr, err := gl.argAsString(convertArg) + if err != nil { + return err + } + gl.gremlin = append(gl.gremlin, argStr) + } + gl.gremlin = append(gl.gremlin, ")") + return nil +} + +func (gl *GremlinLang) argAsString(arg interface{}) (string, error) { + if arg == nil { + return "null", nil + } + // we are concerned with both single and double quotes and %q in fmt only escapes double quotes + escapeQuotes := strings.NewReplacer(`'`, `\'`, `"`, `\"`) + + switch v := arg.(type) { + case string: + return fmt.Sprintf("\"%s\"", escapeQuotes.Replace(v)), nil + case bool: + return strconv.FormatBool(v), nil + case int8, uint8: + return fmt.Sprintf("%dB", v), nil + case int16: + return fmt.Sprintf("%dS", v), nil + case int32, uint16: + return fmt.Sprintf("%d", v), nil + case int: + if v <= math.MaxInt32 && v >= math.MinInt32 { + return fmt.Sprintf("%d", v), nil + } else { + return fmt.Sprintf("%dL", v), nil + } + case int64, uint32: + return fmt.Sprintf("%dL", v), nil + case uint, uint64, *big.Int: + return fmt.Sprintf("%dN", v), nil + case float32: + if math.IsNaN(float64(v)) { + return "NaN", nil + } + if math.IsInf(float64(v), 1) { + return "Infinity", nil + } + if math.IsInf(float64(v), -1) { + return "-Infinity", nil + } + return fmt.Sprintf("%vF", v), nil + case float64: + if math.IsNaN(v) { + return "NaN", nil + } + if math.IsInf(v, 1) { + return "Infinity", nil + } + if math.IsInf(v, -1) { + return "-Infinity", nil + } + return fmt.Sprintf("%vD", v), nil + case *SimpleSet: + return gl.translateSet(v.ToSlice()) + case BigDecimal: + return fmt.Sprintf("%vM", v.Value()), nil + case *BigDecimal: + return fmt.Sprintf("%vM", v.Value()), nil + case time.Time: + return fmt.Sprintf("datetime(\"%v\")", v.Format(time.RFC3339)), nil + case cardinality, column, direction, operator, order, pick, pop, barrier, scope, t, merge, gType: + name := reflect.ValueOf(v).Type().Name() + return fmt.Sprintf("%s.%s", strings.ToUpper(name[:1])+name[1:], v), nil + case dt: + name := reflect.ValueOf(v).Type().Name() + return fmt.Sprintf("%s.%s", strings.ToUpper(name), v), nil + case *Vertex: + id, _ := gl.argAsString(v.Id) + return fmt.Sprintf("new ReferenceVertex(%s,\"%s\")", escapeQuotes.Replace(id), escapeQuotes.Replace(v.Label)), nil Review Comment: Unfortunately not (no coverage in feature tests either). I've updated to make it consistent with other languages, but I think I'll revisit this later separately from this PR, and check the existing coverage for java/python as well. -- 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]
