From e8f17b1602ad8583ce8750fd411cadd9256de961 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Thu, 16 Nov 2017 19:09:56 +0000 Subject: [PATCH] =?UTF-8?q?Add=20=E2=80=98comp=E2=80=99=20package=20for=20?= =?UTF-8?q?comparing=20ordering=20of=20data=20types?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- util/comp/comp.go | 208 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 208 insertions(+) create mode 100644 util/comp/comp.go diff --git a/util/comp/comp.go b/util/comp/comp.go new file mode 100644 index 00000000..4a8f7eb6 --- /dev/null +++ b/util/comp/comp.go @@ -0,0 +1,208 @@ +// Copyright © 2016 Abcum Ltd +// +// Licensed 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 comp + +import ( + "sort" + "strings" + "time" + + "golang.org/x/text/collate" + + "github.com/abcum/surreal/sql" +) + +func Comp(a, b interface{}, expr *sql.Order) int { + + switch x := a.(type) { + + case nil: + + switch b.(type) { + case nil: + return 0 + } + + case bool: + + switch y := b.(type) { + case nil: + return 1 + case bool: + if x == y { + return 0 + } else if !x && y { + return -1 + } else if x && !y { + return +1 + } + } + + case int64: + + switch y := b.(type) { + case nil, bool: + return 1 + case int64: + if x == y { + return 0 + } else if x < y { + return -1 + } else if x > y { + return +1 + } + case float64: + f := float64(x) + if f == y { + return 0 + } else if f < y { + return -1 + } else if f > y { + return +1 + } + } + + case float64: + + switch y := b.(type) { + case nil, bool: + return 1 + case int64: + f := float64(y) + if x == f { + return 0 + } else if x < f { + return -1 + } else if x > f { + return +1 + } + case float64: + if x == y { + return 0 + } else if x < y { + return -1 + } else if x > y { + return +1 + } + } + + case time.Time: + + switch y := b.(type) { + case nil, bool, int64, float64: + return 1 + case time.Time: + t1 := x.UTC().UnixNano() + t2 := y.UTC().UnixNano() + if t1 == t2 { + return 0 + } else if t1 < t2 { + return -1 + } else if t1 > t2 { + return +1 + } + } + + case string: + + switch y := b.(type) { + case nil, bool, int64, float64, time.Time: + return 1 + case string: + if expr.Tag.IsRoot() { + return strings.Compare(x, y) + } else { + c := collate.New(expr.Tag, collate.OptionsFromTag(expr.Tag), collate.Loose) + return c.CompareString(x, y) + } + } + + case *sql.Thing: + + switch y := b.(type) { + case nil, bool, int64, float64, time.Time, string: + return 1 + case *sql.Thing: + if c := strings.Compare(x.TB, y.TB); c == 0 { + return Comp(x.ID, y.ID, expr) + } else { + return c + } + } + + case []interface{}: + + switch y := b.(type) { + case nil, bool, int64, float64, time.Time, string, *sql.Thing: + return 1 + case []interface{}: + + for i := 0; i < len(x) && i < len(y); i++ { + if c := Comp(x[i], y[i], expr); c != 0 { + return c + } + } + + return len(x) - len(y) + + } + + case map[string]interface{}: + + switch y := b.(type) { + case nil, bool, int64, float64, time.Time, string, *sql.Thing, []interface{}: + return 1 + case map[string]interface{}: + + var ke = make([]string, 0) + var me = make(map[string]bool) + + for k := range x { + if !me[k] { + me[k] = true + ke = append(ke, k) + } + } + for k := range y { + if !me[k] { + me[k] = true + ke = append(ke, k) + } + } + + sort.Strings(ke) + + for i := 0; i < len(x) && i < len(y); i++ { + k := ke[i] + if x[k] != nil && y[k] == nil { + return -1 + } + if x[k] == nil && y[k] != nil { + return 1 + } + if c := Comp(x[k], y[k], expr); c != 0 { + return c + } + } + + return len(x) - len(y) + + } + + } + + return -1 + +}