Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ext): restore image and geo functions #3045

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}

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

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

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

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

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

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

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

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

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

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))
}
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))
}
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))
}
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))
}
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))
}
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))
}
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))
}
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))
}
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))
}
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))
}
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
}
lo, ok := args[1].(float64)
if !ok {
return fmt.Errorf("arg[1] is not a float, got %v", args[1]), false
}
return geohash.Encode(la, lo), true
}

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
}
lo, ok := args[1].(float64)
if !ok {
return fmt.Errorf("arg[1] is not a float, got %v", args[1]), false
}
return geohash.EncodeInt(la, lo), true
}

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
}
if err := geohash.Validate(hash); nil != err {
return err, false
}
la, lo := geohash.Decode(hash)
return position{Longitude: lo, Latitude: la}, true
}

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
}
la, lo := geohash.DecodeInt(hash)
return position{Longitude: lo, Latitude: la}, true
}

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
}
if err := geohash.Validate(hash); nil != err {
return err, false
}
return geohash.BoundingBox(hash), true
}

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
}
return geohash.BoundingBoxInt(hash), true
}

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
}
if err := geohash.Validate(hash); nil != err {
return err, false
}
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
}

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

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
}
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

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

View check run for this annotation

Codecov / codecov/patch

extensions/functions/geohash/geohash.go#L269

Added line #L269 was not covered by tests
} 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#L273

Added line #L273 was not covered by tests
}
}
return geohash.NeighborInt(hash, directionCode), true
}

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
}
if err := geohash.Validate(hash); nil != err {
return err, false
}
return geohash.Neighbors(hash), true
}

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
}
return geohash.NeighborsInt(hash), true
}
Loading
Loading