Skip to content

Commit

Permalink
Merge pull request #91 from planetlabs/representation
Browse files Browse the repository at this point in the history
Add string representation for expressions
  • Loading branch information
tschaub authored Jan 29, 2025
2 parents c5e6b42 + af5d3a6 commit 13e13fd
Show file tree
Hide file tree
Showing 24 changed files with 188 additions and 215 deletions.
8 changes: 8 additions & 0 deletions filter/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func (e *ArrayComparison) MarshalJSON() ([]byte, error) {
return marshalOp(e.Name, args)
}

func (e *ArrayComparison) String() string {
return toString(e)
}

type ArrayItemExpression interface {
Expression
arrayItemExpression()
Expand Down Expand Up @@ -84,3 +88,7 @@ func decodeArray(values []any) (Array, error) {
}
return items, nil
}

func (a Array) String() string {
return sliceToString(a)
}
23 changes: 2 additions & 21 deletions filter/array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,14 @@
package filter_test

import (
"encoding/json"
"fmt"
"testing"

"github.com/planetlabs/go-ogc/filter"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestArrayComparison(t *testing.T) {
cases := []struct {
filter *filter.Filter
data string
}{
cases := []*FilterCase{
{
filter: &filter.Filter{
Expression: &filter.ArrayComparison{
Expand Down Expand Up @@ -92,22 +86,9 @@ func TestArrayComparison(t *testing.T) {
},
}

schema := getSchema(t)
for i, c := range cases {
t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) {
data, err := json.Marshal(c.filter)
require.NoError(t, err)
assert.JSONEq(t, c.data, string(data))

v := map[string]any{}
require.NoError(t, json.Unmarshal(data, &v))
if err := schema.Validate(v); err != nil {
t.Errorf("failed to validate\n%#v", err)
}

filter := &filter.Filter{}
require.NoError(t, json.Unmarshal([]byte(c.data), filter))
assert.Equal(t, c.filter, filter)
assertFilterIO(t, c)
})
}
}
7 changes: 7 additions & 0 deletions filter/boolean.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,10 @@ func (e *Boolean) MarshalJSON() ([]byte, error) {
}
return []byte("false"), nil
}

func (e *Boolean) String() string {
if e.Value {
return "true"
}
return "false"
}
23 changes: 2 additions & 21 deletions filter/boolean_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,14 @@
package filter_test

import (
"encoding/json"
"fmt"
"testing"

"github.com/planetlabs/go-ogc/filter"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestBoolean(t *testing.T) {
cases := []struct {
filter *filter.Filter
data string
}{
cases := []*FilterCase{
{
filter: &filter.Filter{
Expression: &filter.Boolean{true},
Expand All @@ -43,22 +37,9 @@ func TestBoolean(t *testing.T) {
},
}

schema := getSchema(t)
for i, c := range cases {
t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) {
data, err := json.Marshal(c.filter)
require.Nil(t, err)
assert.JSONEq(t, c.data, string(data))

var v bool
require.NoError(t, json.Unmarshal(data, &v))
if err := schema.Validate(v); err != nil {
t.Errorf("failed to validate\n%#v", err)
}

filter := &filter.Filter{}
require.Nil(t, json.Unmarshal([]byte(c.data), filter))
assert.Equal(t, c.filter, filter)
assertFilterIO(t, c)
})
}
}
12 changes: 12 additions & 0 deletions filter/character.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ func (e *CaseInsensitive) MarshalJSON() ([]byte, error) {
return json.Marshal(m)
}

func (e *CaseInsensitive) String() string {
return toString(e)
}

type AccentInsensitive struct {
Value CharacterExpression
}
Expand All @@ -83,6 +87,10 @@ func (e *AccentInsensitive) MarshalJSON() ([]byte, error) {
return json.Marshal(m)
}

func (e *AccentInsensitive) String() string {
return toString(e)
}

type String struct {
Value string
}
Expand All @@ -104,3 +112,7 @@ func (*String) arrayItemExpression() {}
func (e *String) MarshalJSON() ([]byte, error) {
return json.Marshal(e.Value)
}

func (e *String) String() string {
return toString(e)
}
23 changes: 2 additions & 21 deletions filter/character_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,14 @@
package filter_test

import (
"encoding/json"
"fmt"
"testing"

"github.com/planetlabs/go-ogc/filter"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestCharacter(t *testing.T) {
cases := []struct {
filter *filter.Filter
data string
}{
cases := []*FilterCase{
{
filter: &filter.Filter{
Expression: &filter.Comparison{
Expand Down Expand Up @@ -70,22 +64,9 @@ func TestCharacter(t *testing.T) {
},
}

schema := getSchema(t)
for i, c := range cases {
t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) {
data, err := json.Marshal(c.filter)
require.Nil(t, err)
assert.JSONEq(t, c.data, string(data))

v := map[string]any{}
require.NoError(t, json.Unmarshal(data, &v))
if err := schema.Validate(v); err != nil {
t.Errorf("failed to validate\n%#v", err)
}

filter := &filter.Filter{}
require.Nil(t, json.Unmarshal([]byte(c.data), filter))
assert.Equal(t, c.filter, filter)
assertFilterIO(t, c)
})
}
}
28 changes: 26 additions & 2 deletions filter/comparison.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,12 @@ func (e *Comparison) MarshalJSON() ([]byte, error) {
return marshalOp(e.Name, args)
}

func (e *Comparison) String() string {
return toString(e)
}

type Like struct {
String CharacterExpression
Value CharacterExpression
Pattern PatternExpression
}

Expand All @@ -66,10 +70,14 @@ func (*Like) scalarExpression() {}
func (*Like) booleanExpression() {}

func (e *Like) MarshalJSON() ([]byte, error) {
args := []Expression{e.String, e.Pattern}
args := []Expression{e.Value, e.Pattern}
return marshalOp(likeOp, args)
}

func (e *Like) String() string {
return toString(e)
}

type Between struct {
Value NumericExpression
Low NumericExpression
Expand All @@ -91,6 +99,10 @@ func (e *Between) MarshalJSON() ([]byte, error) {
return marshalOp(betweenOp, args)
}

func (e *Between) String() string {
return toString(e)
}

type ScalarList []ScalarExpression

var (
Expand All @@ -100,6 +112,10 @@ var (
func (ScalarList) expression() {}
func (ScalarList) scalarExpression() {}

func (e ScalarList) String() string {
return sliceToString(e)
}

type In struct {
Item ScalarExpression
List ScalarList
Expand All @@ -120,6 +136,10 @@ func (e *In) MarshalJSON() ([]byte, error) {
return marshalOp(inOp, args)
}

func (e *In) String() string {
return toString(e)
}

type IsNull struct {
Value Expression
}
Expand All @@ -138,3 +158,7 @@ func (e *IsNull) MarshalJSON() ([]byte, error) {
args := []Expression{e.Value}
return marshalOp(isNullOp, args)
}

func (e *IsNull) String() string {
return toString(e)
}
27 changes: 4 additions & 23 deletions filter/comparison_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,14 @@
package filter_test

import (
"encoding/json"
"fmt"
"testing"

"github.com/planetlabs/go-ogc/filter"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestComparison(t *testing.T) {
cases := []struct {
filter *filter.Filter
data string
}{
cases := []*FilterCase{
{
filter: &filter.Filter{
Expression: &filter.Comparison{
Expand Down Expand Up @@ -123,7 +117,7 @@ func TestComparison(t *testing.T) {
{
filter: &filter.Filter{
Expression: &filter.Like{
String: &filter.Property{"name"},
Value: &filter.Property{"name"},
Pattern: &filter.CaseInsensitive{&filter.String{"park"}},
},
},
Expand All @@ -135,7 +129,7 @@ func TestComparison(t *testing.T) {
{
filter: &filter.Filter{
Expression: &filter.Like{
String: &filter.Property{"name"},
Value: &filter.Property{"name"},
Pattern: &filter.AccentInsensitive{&filter.String{"Noël"}},
},
},
Expand Down Expand Up @@ -183,22 +177,9 @@ func TestComparison(t *testing.T) {
},
}

schema := getSchema(t)
for i, c := range cases {
t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) {
data, err := json.Marshal(c.filter)
require.NoError(t, err)
assert.JSONEq(t, c.data, string(data))

v := map[string]any{}
require.NoError(t, json.Unmarshal(data, &v))
if err := schema.Validate(v); err != nil {
t.Errorf("failed to validate\n%#v", err)
}

filter := &filter.Filter{}
require.NoError(t, json.Unmarshal([]byte(c.data), filter))
assert.Equal(t, c.filter, filter)
assertFilterIO(t, c)
})
}
}
1 change: 1 addition & 0 deletions filter/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

type Expression interface {
expression()
String() string
}

type ScalarExpression interface {
Expand Down
4 changes: 4 additions & 0 deletions filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,7 @@ func (f *Filter) UnmarshalJSON(data []byte) error {
f.Expression = booleanExpression
return nil
}

func (e *Filter) String() string {
return toString(e)
}
Loading

0 comments on commit 13e13fd

Please sign in to comment.