Skip to content

Commit

Permalink
feat(ext): restore image and geo functions
Browse files Browse the repository at this point in the history
Signed-off-by: Jiyong Huang <huangjy@emqx.io>
  • Loading branch information
ngjaying committed Jul 24, 2024
1 parent 7ff523a commit 4daea01
Show file tree
Hide file tree
Showing 12 changed files with 1,327 additions and 0 deletions.
296 changes: 296 additions & 0 deletions extensions/functions/geohash/geohash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,296 @@
// Copyright 2021-2024 EMQ Technologies Co., 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 main

import (
"fmt"

"github.com/mmcloughlin/geohash"

"github.com/lf-edge/ekuiper/contract/v2/api"
)

type (
geohashEncode struct{}
geohashEncodeInt struct{}
geohashDecode struct{}
geohashDecodeInt struct{}
geohashBoundingBox struct{}
geohashBoundingBoxInt struct{}
geohashNeighbor struct{}
geohashNeighborInt struct{}
geohashNeighbors struct{}
geohashNeighborsInt struct{}
position struct {
Longitude float64
Latitude float64
}
)

var (
GeohashEncode geohashEncode
GeohashEncodeInt geohashEncodeInt
GeohashDecode geohashDecode
GeohashDecodeInt geohashDecodeInt
GeohashBoundingBox geohashBoundingBox
GeohashBoundingBoxInt geohashBoundingBoxInt
GeohashNeighbor geohashNeighbor
GeohashNeighborInt geohashNeighborInt
GeohashNeighbors geohashNeighbors
GeohashNeighborsInt geohashNeighborsInt
g_direction = map[string]geohash.Direction{
"North": geohash.North,
"NorthEast": geohash.NorthEast,
"East": geohash.East,
"SouthEast": geohash.SouthEast,
"South": geohash.South,
"SouthWest": geohash.SouthWest,
"West": geohash.West,
"NorthWest": geohash.NorthWest,
}
)

func (r *geohashEncode) IsAggregate() bool {
return false

Check warning on line 66 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L65-L66

Added lines #L65 - L66 were not covered by tests
}

func (r *geohashEncodeInt) IsAggregate() bool {
return false

Check warning on line 70 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L69-L70

Added lines #L69 - L70 were not covered by tests
}

func (r *geohashDecode) IsAggregate() bool {
return false

Check warning on line 74 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L73-L74

Added lines #L73 - L74 were not covered by tests
}

func (r *geohashDecodeInt) IsAggregate() bool {
return false

Check warning on line 78 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L77-L78

Added lines #L77 - L78 were not covered by tests
}

func (r *geohashBoundingBox) IsAggregate() bool {
return false

Check warning on line 82 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L81-L82

Added lines #L81 - L82 were not covered by tests
}

func (r *geohashBoundingBoxInt) IsAggregate() bool {
return false

Check warning on line 86 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L85-L86

Added lines #L85 - L86 were not covered by tests
}

func (r *geohashNeighbor) IsAggregate() bool {
return false

Check warning on line 90 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L89-L90

Added lines #L89 - L90 were not covered by tests
}

func (r *geohashNeighborInt) IsAggregate() bool {
return false

Check warning on line 94 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L93-L94

Added lines #L93 - L94 were not covered by tests
}

func (r *geohashNeighbors) IsAggregate() bool {
return false

Check warning on line 98 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L97-L98

Added lines #L97 - L98 were not covered by tests
}

func (r *geohashNeighborsInt) IsAggregate() bool {
return false

Check warning on line 102 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L101-L102

Added lines #L101 - L102 were not covered by tests
}

func (r *geohashEncode) Validate(args []any) error {
if len(args) != 2 {
return fmt.Errorf("The geohashEncode function supports 2 parameters, but got %d", len(args))

Check warning on line 107 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L105-L107

Added lines #L105 - L107 were not covered by tests
}
return nil

Check warning on line 109 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L109

Added line #L109 was not covered by tests
}

func (r *geohashEncodeInt) Validate(args []any) error {
if len(args) != 2 {
return fmt.Errorf("The geohashEncodeInt function supports 2 parameters, but got %d", len(args))

Check warning on line 114 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L112-L114

Added lines #L112 - L114 were not covered by tests
}
return nil

Check warning on line 116 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L116

Added line #L116 was not covered by tests
}

func (r *geohashDecode) Validate(args []any) error {
if len(args) != 1 {
return fmt.Errorf("The geohashDecode function supports 1 parameters, but got %d", len(args))

Check warning on line 121 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L119-L121

Added lines #L119 - L121 were not covered by tests
}
return nil

Check warning on line 123 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L123

Added line #L123 was not covered by tests
}

func (r *geohashDecodeInt) Validate(args []any) error {
if len(args) != 1 {
return fmt.Errorf("The geohashDecodeInt function supports 1 parameters, but got %d", len(args))

Check warning on line 128 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L126-L128

Added lines #L126 - L128 were not covered by tests
}
return nil

Check warning on line 130 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L130

Added line #L130 was not covered by tests
}

func (r *geohashBoundingBox) Validate(args []any) error {
if len(args) != 1 {
return fmt.Errorf("The geohashBoundingBox function supports 1 parameters, but got %d", len(args))

Check warning on line 135 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L133-L135

Added lines #L133 - L135 were not covered by tests
}
return nil

Check warning on line 137 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L137

Added line #L137 was not covered by tests
}

func (r *geohashBoundingBoxInt) Validate(args []any) error {
if len(args) != 1 {
return fmt.Errorf("The geohashBoundingBoxInt function supports 1 parameters, but got %d", len(args))

Check warning on line 142 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L140-L142

Added lines #L140 - L142 were not covered by tests
}
return nil

Check warning on line 144 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L144

Added line #L144 was not covered by tests
}

func (r *geohashNeighbor) Validate(args []any) error {
if len(args) != 2 {
return fmt.Errorf("The geohashNeighbor function supports 2 parameters, but got %d", len(args))

Check warning on line 149 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L147-L149

Added lines #L147 - L149 were not covered by tests
}
return nil

Check warning on line 151 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L151

Added line #L151 was not covered by tests
}

func (r *geohashNeighborInt) Validate(args []any) error {
if len(args) != 2 {
return fmt.Errorf("The geohashNeighborInt function supports 2 parameters, but got %d", len(args))

Check warning on line 156 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L154-L156

Added lines #L154 - L156 were not covered by tests
}
return nil

Check warning on line 158 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L158

Added line #L158 was not covered by tests
}

func (r *geohashNeighbors) Validate(args []any) error {
if len(args) != 1 {
return fmt.Errorf("The geohashNeighbors function supports 1 parameters, but got %d", len(args))

Check warning on line 163 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L161-L163

Added lines #L161 - L163 were not covered by tests
}
return nil

Check warning on line 165 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L165

Added line #L165 was not covered by tests
}

func (r *geohashNeighborsInt) Validate(args []any) error {
if len(args) != 1 {
return fmt.Errorf("The geohashNeighborsInt function supports 1 parameters, but got %d", len(args))

Check warning on line 170 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L168-L170

Added lines #L168 - L170 were not covered by tests
}
return nil

Check warning on line 172 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L172

Added line #L172 was not covered by tests
}

func (r *geohashEncode) Exec(args []any, _ api.FunctionContext) (any, bool) {
la, ok := args[0].(float64)
if !ok {
return fmt.Errorf("arg[0] is not a float, got %v", args[0]), false

Check warning on line 178 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L175-L178

Added lines #L175 - L178 were not covered by tests
}
lo, ok := args[1].(float64)
if !ok {
return fmt.Errorf("arg[1] is not a float, got %v", args[1]), false

Check warning on line 182 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L180-L182

Added lines #L180 - L182 were not covered by tests
}
return geohash.Encode(la, lo), true

Check warning on line 184 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L184

Added line #L184 was not covered by tests
}

func (r *geohashEncodeInt) Exec(args []any, _ api.FunctionContext) (any, bool) {
la, ok := args[0].(float64)
if !ok {
return fmt.Errorf("arg[0] is not a float, got %v", args[0]), false

Check warning on line 190 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L187-L190

Added lines #L187 - L190 were not covered by tests
}
lo, ok := args[1].(float64)
if !ok {
return fmt.Errorf("arg[1] is not a float, got %v", args[1]), false

Check warning on line 194 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L192-L194

Added lines #L192 - L194 were not covered by tests
}
return geohash.EncodeInt(la, lo), true

Check warning on line 196 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L196

Added line #L196 was not covered by tests
}

func (r *geohashDecode) Exec(args []any, _ api.FunctionContext) (any, bool) {
hash, ok := args[0].(string)
if !ok || 0 == len(hash) {
return fmt.Errorf("arg[0] is not a string, got %v", args[0]), false

Check warning on line 202 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L199-L202

Added lines #L199 - L202 were not covered by tests
}
if err := geohash.Validate(hash); nil != err {
return err, false

Check warning on line 205 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L204-L205

Added lines #L204 - L205 were not covered by tests
}
la, lo := geohash.Decode(hash)
return position{Longitude: lo, Latitude: la}, true

Check warning on line 208 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L207-L208

Added lines #L207 - L208 were not covered by tests
}

func (r *geohashDecodeInt) Exec(args []any, _ api.FunctionContext) (any, bool) {
hash, ok := args[0].(uint64)
if !ok {
return fmt.Errorf("arg[0] is not a bigint, got %v", args[0]), false

Check warning on line 214 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L211-L214

Added lines #L211 - L214 were not covered by tests
}
la, lo := geohash.DecodeInt(hash)
return position{Longitude: lo, Latitude: la}, true

Check warning on line 217 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L216-L217

Added lines #L216 - L217 were not covered by tests
}

func (r *geohashBoundingBox) Exec(args []any, _ api.FunctionContext) (any, bool) {
hash, ok := args[0].(string)
if !ok || 0 == len(hash) {
return fmt.Errorf("arg[0] is not a string, got %v", args[0]), false

Check warning on line 223 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L220-L223

Added lines #L220 - L223 were not covered by tests
}
if err := geohash.Validate(hash); nil != err {
return err, false

Check warning on line 226 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L225-L226

Added lines #L225 - L226 were not covered by tests
}
return geohash.BoundingBox(hash), true

Check warning on line 228 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L228

Added line #L228 was not covered by tests
}

func (r *geohashBoundingBoxInt) Exec(args []any, _ api.FunctionContext) (any, bool) {
hash, ok := args[0].(uint64)
if !ok {
return fmt.Errorf("arg[0] is not a bigint, got %v", args[0]), false

Check warning on line 234 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L231-L234

Added lines #L231 - L234 were not covered by tests
}
return geohash.BoundingBoxInt(hash), true

Check warning on line 236 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L236

Added line #L236 was not covered by tests
}

func (r *geohashNeighbor) Exec(args []any, _ api.FunctionContext) (any, bool) {
hash, ok := args[0].(string)
if !ok || 0 == len(hash) {
return fmt.Errorf("arg[0] is not a string, got %v", args[0]), false

Check warning on line 242 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L239-L242

Added lines #L239 - L242 were not covered by tests
}
if err := geohash.Validate(hash); nil != err {
return err, false

Check warning on line 245 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L244-L245

Added lines #L244 - L245 were not covered by tests
}
var directionCode geohash.Direction
direction, ok := args[1].(string)
if !ok || 0 == len(direction) {
return fmt.Errorf("arg[1] is not a string, got %v", args[1]), false
} else {
directionCode, ok = g_direction[direction]
if !ok {
return fmt.Errorf("arg[1] is valid, got %v", args[1]), false

Check warning on line 254 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L247-L254

Added lines #L247 - L254 were not covered by tests
}

}
return geohash.Neighbor(hash, directionCode), true

Check warning on line 258 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L258

Added line #L258 was not covered by tests
}

func (r *geohashNeighborInt) Exec(args []any, _ api.FunctionContext) (any, bool) {
hash, ok := args[0].(uint64)
if !ok {
return fmt.Errorf("arg[0] is not a bigint, got %v", args[0]), false

Check warning on line 264 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L261-L264

Added lines #L261 - L264 were not covered by tests
}
var directionCode geohash.Direction
direction, ok := args[1].(string)
if !ok || 0 == len(direction) {
return fmt.Errorf("arg[1] is not a string, got %v", args[1]), false
} else {
directionCode, ok = g_direction[direction]
if !ok {
return fmt.Errorf("arg[1] is valid, got %v", args[1]), false

Check warning on line 273 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L266-L273

Added lines #L266 - L273 were not covered by tests
}
}
return geohash.NeighborInt(hash, directionCode), true

Check warning on line 276 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L276

Added line #L276 was not covered by tests
}

func (r *geohashNeighbors) Exec(args []any, _ api.FunctionContext) (any, bool) {
hash, ok := args[0].(string)
if !ok || 0 == len(hash) {
return fmt.Errorf("arg[0] is not a string, got %v", args[0]), false

Check warning on line 282 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L279-L282

Added lines #L279 - L282 were not covered by tests
}
if err := geohash.Validate(hash); nil != err {
return err, false

Check warning on line 285 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L284-L285

Added lines #L284 - L285 were not covered by tests
}
return geohash.Neighbors(hash), true

Check warning on line 287 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L287

Added line #L287 was not covered by tests
}

func (r *geohashNeighborsInt) Exec(args []any, _ api.FunctionContext) (any, bool) {
hash, ok := args[0].(uint64)
if !ok {
return fmt.Errorf("arg[0] is not a bigint, got %v", args[0]), false

Check warning on line 293 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L290-L293

Added lines #L290 - L293 were not covered by tests
}
return geohash.NeighborsInt(hash), true

Check warning on line 295 in extensions/functions/geohash/geohash.go

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L295

Added line #L295 was not covered by tests
}
Loading

0 comments on commit 4daea01

Please sign in to comment.