mirror of
https://github.com/foomo/contentfulvalidation.git
synced 2025-10-16 12:25:37 +00:00
revert internal type
This commit is contained in:
parent
932c7e493f
commit
62412959b1
232
constants/vo.go
232
constants/vo.go
@ -1,148 +1,148 @@
|
||||
package constants
|
||||
|
||||
import (
|
||||
"github.com/RoaringBitmap/roaring"
|
||||
)
|
||||
// import (
|
||||
// "github.com/RoaringBitmap/roaring"
|
||||
// )
|
||||
|
||||
type Severity string
|
||||
type Health string
|
||||
type QueryError string
|
||||
|
||||
type (
|
||||
AttributeID string
|
||||
AttributeValueID string
|
||||
AttributeType string
|
||||
// type (
|
||||
// AttributeID string
|
||||
// AttributeValueID string
|
||||
// AttributeType string
|
||||
|
||||
// Locale like en or en_us
|
||||
Locale string
|
||||
LocalizedString map[Locale]string
|
||||
)
|
||||
// // Locale like en or en_us
|
||||
// Locale string
|
||||
// LocalizedString map[Locale]string
|
||||
// )
|
||||
|
||||
type Attributes map[AttributeID]AttributeDefinition
|
||||
// type Attributes map[AttributeID]AttributeDefinition
|
||||
|
||||
// AttributeDefinition describes an attribute.
|
||||
type AttributeDefinition struct {
|
||||
ID AttributeID `json:"id"`
|
||||
Type AttributeType `json:"type"`
|
||||
EnumStrings map[AttributeValueID]*string `json:"enumStrings,omitempty"`
|
||||
Meta AttributeMeta `json:"meta,omitempty"`
|
||||
MetaValues map[AttributeValueID]AttributeMeta `json:"metaValues,omitempty"`
|
||||
StepSize int `json:"stepSize,omitempty"`
|
||||
Mandatory bool `json:"mandatory,omitempty"`
|
||||
}
|
||||
// // AttributeDefinition describes an attribute.
|
||||
// type AttributeDefinition struct {
|
||||
// ID AttributeID `json:"id"`
|
||||
// Type AttributeType `json:"type"`
|
||||
// EnumStrings map[AttributeValueID]*string `json:"enumStrings,omitempty"`
|
||||
// Meta AttributeMeta `json:"meta,omitempty"`
|
||||
// MetaValues map[AttributeValueID]AttributeMeta `json:"metaValues,omitempty"`
|
||||
// StepSize int `json:"stepSize,omitempty"`
|
||||
// Mandatory bool `json:"mandatory,omitempty"`
|
||||
// }
|
||||
|
||||
// AttributeMeta models meta information for an attribute.
|
||||
type AttributeMeta struct {
|
||||
Label LocalizedString `json:"label,omitempty"`
|
||||
Description LocalizedString `json:"description,omitempty"`
|
||||
Custom map[string]string `json:"custom,omitempty"`
|
||||
SortingRank map[string]int `json:"sortingRank,omitempty"`
|
||||
}
|
||||
// // AttributeMeta models meta information for an attribute.
|
||||
// type AttributeMeta struct {
|
||||
// Label LocalizedString `json:"label,omitempty"`
|
||||
// Description LocalizedString `json:"description,omitempty"`
|
||||
// Custom map[string]string `json:"custom,omitempty"`
|
||||
// SortingRank map[string]int `json:"sortingRank,omitempty"`
|
||||
// }
|
||||
|
||||
// Query structure
|
||||
type Query struct {
|
||||
Explanation string `json:"explanation,omitempty"`
|
||||
Operation Operation `json:"operation"`
|
||||
Elements []*QueryElement `json:"elements"`
|
||||
}
|
||||
// // Query structure
|
||||
// type Query struct {
|
||||
// Explanation string `json:"explanation,omitempty"`
|
||||
// Operation Operation `json:"operation"`
|
||||
// Elements []*QueryElement `json:"elements"`
|
||||
// }
|
||||
|
||||
// Operation defines how to compare bitmaps
|
||||
type Operation string
|
||||
// // Operation defines how to compare bitmaps
|
||||
// type Operation string
|
||||
|
||||
// QueryElement structure
|
||||
type QueryElement struct {
|
||||
Matcher *Matcher `json:"matcher,omitempty"`
|
||||
Query *Query `json:"query,omitempty"`
|
||||
}
|
||||
// // QueryElement structure
|
||||
// type QueryElement struct {
|
||||
// Matcher *Matcher `json:"matcher,omitempty"`
|
||||
// Query *Query `json:"query,omitempty"`
|
||||
// }
|
||||
|
||||
// Matcher structure
|
||||
// Identifies a bitmap
|
||||
// the result of each match operation is a bitmap of entity ids
|
||||
type Matcher struct {
|
||||
Attribute AttributeID `json:"attribute,omitempty"`
|
||||
Explanation string `json:"explanation,omitempty"`
|
||||
// // Matcher structure
|
||||
// // Identifies a bitmap
|
||||
// // the result of each match operation is a bitmap of entity ids
|
||||
// type Matcher struct {
|
||||
// Attribute AttributeID `json:"attribute,omitempty"`
|
||||
// Explanation string `json:"explanation,omitempty"`
|
||||
|
||||
// strings
|
||||
StringIn *StringIn `json:"stringIn,omitempty"`
|
||||
StringAllIn *StringAllIn `json:"stringAllIn,omitempty"`
|
||||
StringNotIn *StringNotIn `json:"stringNotIn,omitempty"`
|
||||
StringEquals *StringEquals `json:"stringEquals,omitempty"`
|
||||
StringNotEquals *StringNotEquals `json:"stringNotEquals,omitempty"`
|
||||
// // strings
|
||||
// StringIn *StringIn `json:"stringIn,omitempty"`
|
||||
// StringAllIn *StringAllIn `json:"stringAllIn,omitempty"`
|
||||
// StringNotIn *StringNotIn `json:"stringNotIn,omitempty"`
|
||||
// StringEquals *StringEquals `json:"stringEquals,omitempty"`
|
||||
// StringNotEquals *StringNotEquals `json:"stringNotEquals,omitempty"`
|
||||
|
||||
// integers
|
||||
IntInRange *IntInRange `json:"intInRange,omitempty"`
|
||||
IntFrom *IntFrom `json:"intFrom,omitempty"`
|
||||
IntTo *IntTo `json:"intTo,omitempty"`
|
||||
IntEquals *IntEquals `json:"intEquals,omitempty"`
|
||||
IntNotEquals *IntNotEquals `json:"intNotEquals,omitempty"`
|
||||
// // integers
|
||||
// IntInRange *IntInRange `json:"intInRange,omitempty"`
|
||||
// IntFrom *IntFrom `json:"intFrom,omitempty"`
|
||||
// IntTo *IntTo `json:"intTo,omitempty"`
|
||||
// IntEquals *IntEquals `json:"intEquals,omitempty"`
|
||||
// IntNotEquals *IntNotEquals `json:"intNotEquals,omitempty"`
|
||||
|
||||
// booleans
|
||||
BoolEquals *BoolEquals `json:"boolEquals,omitempty"`
|
||||
// // booleans
|
||||
// BoolEquals *BoolEquals `json:"boolEquals,omitempty"`
|
||||
|
||||
// bitmap
|
||||
Bitmap *Bitmap `json:"bitmap,omitempty"`
|
||||
}
|
||||
// // bitmap
|
||||
// Bitmap *Bitmap `json:"bitmap,omitempty"`
|
||||
// }
|
||||
|
||||
// StringIn matches if the input value equals any of the strings specified
|
||||
type StringIn struct {
|
||||
Values []string `json:"values"`
|
||||
}
|
||||
// // StringIn matches if the input value equals any of the strings specified
|
||||
// type StringIn struct {
|
||||
// Values []string `json:"values"`
|
||||
// }
|
||||
|
||||
// StringAllIn matches if all values appear in the input
|
||||
type StringAllIn struct {
|
||||
Values []string `json:"values"`
|
||||
}
|
||||
// // StringAllIn matches if all values appear in the input
|
||||
// type StringAllIn struct {
|
||||
// Values []string `json:"values"`
|
||||
// }
|
||||
|
||||
// StringNotIn matches if the input value does not equal any of the strings specified
|
||||
type StringNotIn struct {
|
||||
Values []string `json:"values"`
|
||||
}
|
||||
// // StringNotIn matches if the input value does not equal any of the strings specified
|
||||
// type StringNotIn struct {
|
||||
// Values []string `json:"values"`
|
||||
// }
|
||||
|
||||
// StringEquals matches strings that DO equal the supplied value
|
||||
type StringEquals struct {
|
||||
Value string `json:"value"`
|
||||
}
|
||||
// // StringEquals matches strings that DO equal the supplied value
|
||||
// type StringEquals struct {
|
||||
// Value string `json:"value"`
|
||||
// }
|
||||
|
||||
// StringNotEquals matches strings that DO NOT equal the supplied value
|
||||
type StringNotEquals struct {
|
||||
Value string `json:"value"`
|
||||
}
|
||||
// // StringNotEquals matches strings that DO NOT equal the supplied value
|
||||
// type StringNotEquals struct {
|
||||
// Value string `json:"value"`
|
||||
// }
|
||||
|
||||
// IntInRange matches integers in the given range
|
||||
type IntInRange struct {
|
||||
From int `json:"from"`
|
||||
To int `json:"to"`
|
||||
}
|
||||
// // IntInRange matches integers in the given range
|
||||
// type IntInRange struct {
|
||||
// From int `json:"from"`
|
||||
// To int `json:"to"`
|
||||
// }
|
||||
|
||||
// IntFrom matches integers starting from the given value (>=)
|
||||
type IntFrom struct {
|
||||
From int `json:"from"`
|
||||
}
|
||||
// // IntFrom matches integers starting from the given value (>=)
|
||||
// type IntFrom struct {
|
||||
// From int `json:"from"`
|
||||
// }
|
||||
|
||||
// IntTo matches integers until the given value (<=)
|
||||
type IntTo struct {
|
||||
To int `json:"to"`
|
||||
}
|
||||
// // IntTo matches integers until the given value (<=)
|
||||
// type IntTo struct {
|
||||
// To int `json:"to"`
|
||||
// }
|
||||
|
||||
// IntEquals matches integers exactly (==)
|
||||
type IntEquals struct {
|
||||
Value int `json:"value"`
|
||||
}
|
||||
// // IntEquals matches integers exactly (==)
|
||||
// type IntEquals struct {
|
||||
// Value int `json:"value"`
|
||||
// }
|
||||
|
||||
// IntNotEquals matches integers that do not equal the given value (!=)
|
||||
type IntNotEquals struct {
|
||||
Value int `json:"value"`
|
||||
}
|
||||
// // IntNotEquals matches integers that do not equal the given value (!=)
|
||||
// type IntNotEquals struct {
|
||||
// Value int `json:"value"`
|
||||
// }
|
||||
|
||||
// BoolEquals matches booleans exactly (==)
|
||||
type BoolEquals struct {
|
||||
Value bool `json:"value"`
|
||||
}
|
||||
// // BoolEquals matches booleans exactly (==)
|
||||
// type BoolEquals struct {
|
||||
// Value bool `json:"value"`
|
||||
// }
|
||||
|
||||
// Bitmap allows to use a *roaring.Bitmap directly for a matcher
|
||||
type Bitmap struct {
|
||||
// value is private to hide it from gotsrpc
|
||||
value *roaring.Bitmap `json:"-"`
|
||||
// // Bitmap allows to use a *roaring.Bitmap directly for a matcher
|
||||
// type Bitmap struct {
|
||||
// // value is private to hide it from gotsrpc
|
||||
// value *roaring.Bitmap `json:"-"`
|
||||
|
||||
FacetValue string `json:"facetValue"`
|
||||
}
|
||||
// FacetValue string `json:"facetValue"`
|
||||
// }
|
||||
|
||||
7
go.mod
7
go.mod
@ -3,22 +3,21 @@ module github.com/foomo/contentfulvalidation
|
||||
go 1.20
|
||||
|
||||
require (
|
||||
github.com/RoaringBitmap/roaring v1.3.0
|
||||
github.com/bestbytes/catalogue v0.39.1
|
||||
github.com/foomo/contentful v0.4.4
|
||||
github.com/foomo/contentserver v1.10.2
|
||||
github.com/foomo/gotsrpc/v2 v2.7.2
|
||||
github.com/foomo/keel v0.16.1
|
||||
github.com/go-co-op/gocron v1.33.0
|
||||
github.com/pkg/errors v0.9.1
|
||||
github.com/stretchr/testify v1.8.4
|
||||
go.uber.org/zap v1.25.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/RoaringBitmap/roaring v1.3.0 // indirect
|
||||
github.com/beorn7/perks v1.0.1 // indirect
|
||||
github.com/bits-and-blooms/bitset v1.8.0 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.2.0 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/golang/protobuf v1.5.3 // indirect
|
||||
github.com/google/uuid v1.3.1 // indirect
|
||||
github.com/iancoleman/strcase v0.2.0 // indirect
|
||||
@ -28,7 +27,6 @@ require (
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/mschoch/smat v0.2.0 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/prometheus/client_golang v1.16.0 // indirect
|
||||
github.com/prometheus/client_model v0.3.0 // indirect
|
||||
github.com/prometheus/common v0.42.0 // indirect
|
||||
@ -44,6 +42,5 @@ require (
|
||||
golang.org/x/tools v0.10.0 // indirect
|
||||
google.golang.org/protobuf v1.30.0 // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
moul.io/http2curl v1.0.0 // indirect
|
||||
)
|
||||
|
||||
3
go.sum
3
go.sum
@ -3,6 +3,8 @@ github.com/RoaringBitmap/roaring v1.3.0/go.mod h1:plvDsJQpxOC5bw8LRteu/MLWHsHez/
|
||||
github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A=
|
||||
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
|
||||
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
|
||||
github.com/bestbytes/catalogue v0.39.1 h1:NAbES4bl8hg5a9UAGpTrrLTV/7PE86HEsGd7O5m/fJg=
|
||||
github.com/bestbytes/catalogue v0.39.1/go.mod h1:eyZEeaZJSqc/r78jejYM+h8fkLnS1Kzd+IPzG0hxLH0=
|
||||
github.com/bits-and-blooms/bitset v1.2.0/go.mod h1:gIdJ4wp64HaoK2YrL1Q5/N7Y16edYb8uY+O0FJTyyDA=
|
||||
github.com/bits-and-blooms/bitset v1.8.0 h1:FD+XqgOZDUxxZ8hzoBFuV9+cGWY9CslN6d5MS5JVb4c=
|
||||
github.com/bits-and-blooms/bitset v1.8.0/go.mod h1:7hO7Gc7Pp1vODcmWvKMRA9BNmbv6a/7QIWpPxHddWR8=
|
||||
@ -94,7 +96,6 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU=
|
||||
github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
|
||||
go.opentelemetry.io/otel v1.7.0 h1:Z2lA3Tdch0iDcrhJXDIlC94XE+bxok1F9B+4Lz/lGsM=
|
||||
|
||||
@ -1,49 +0,0 @@
|
||||
package tests_test
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/foomo/contentfulvalidation/constants"
|
||||
"github.com/foomo/contentfulvalidation/validations"
|
||||
|
||||
// testingx "github.com/foomo/go/testing"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
//go:embed data/query.json
|
||||
var queryData []byte
|
||||
|
||||
//go:embed data/attributes.json
|
||||
var attributesData []byte
|
||||
|
||||
func getMockAttributes() constants.Attributes {
|
||||
var attributes constants.Attributes
|
||||
err := json.Unmarshal(attributesData, &attributes)
|
||||
if err != nil {
|
||||
return constants.Attributes{}
|
||||
}
|
||||
return attributes
|
||||
}
|
||||
|
||||
func getMockQuery() constants.Query {
|
||||
var query constants.Query
|
||||
err := json.Unmarshal(queryData, &query)
|
||||
if err != nil {
|
||||
return constants.Query{}
|
||||
}
|
||||
return query
|
||||
}
|
||||
|
||||
func Test_Query(t *testing.T) {
|
||||
// testingx.Tags(t)
|
||||
|
||||
query := getMockQuery()
|
||||
testResult := []constants.QueryError{"Query Field is empty", "Query field value is expired", "Missing field values", "Missing query condition", "Query Field is empty"}
|
||||
queryErrors := validations.ValidateQuery(&query, getMockAttributes())
|
||||
fmt.Println("queryErrors", queryErrors)
|
||||
|
||||
assert.Equal(t, testResult, queryErrors)
|
||||
}
|
||||
@ -4,8 +4,8 @@ import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
catvo "github.com/bestbytes/catalogue/vo"
|
||||
"github.com/foomo/contentful"
|
||||
"github.com/foomo/contentfulvalidation/constants"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
@ -43,8 +43,8 @@ func GetAspectRatio(asset *contentful.AssetNoLocale) (float64, error) {
|
||||
return aspectRatio, nil
|
||||
}
|
||||
|
||||
func LoadQuery(rawQuery *interface{}) (*constants.Query, error) {
|
||||
query := &constants.Query{}
|
||||
func LoadQuery(rawQuery *interface{}) (*catvo.Query, error) {
|
||||
query := &catvo.Query{}
|
||||
errMarshal := loadInterfaceAsJSON(rawQuery, query)
|
||||
if errMarshal != nil {
|
||||
return nil, errMarshal
|
||||
|
||||
@ -1,23 +1,24 @@
|
||||
package validations
|
||||
|
||||
import (
|
||||
catvo "github.com/bestbytes/catalogue/vo"
|
||||
"github.com/foomo/contentfulvalidation/constants"
|
||||
)
|
||||
|
||||
func ValidateQuery(query *constants.Query, attributes constants.Attributes) []constants.QueryError {
|
||||
func ValidateQuery(query *catvo.Query, attributes catvo.Attributes) []constants.QueryError {
|
||||
errors := []constants.QueryError{}
|
||||
|
||||
isValueExpired := func(value string, def constants.AttributeDefinition) {
|
||||
isValueExpired := func(value string, def catvo.AttributeDefinition) {
|
||||
if len(value) < 1 {
|
||||
errors = append(errors, constants.MissingQueryFieldValues)
|
||||
} else {
|
||||
if _, ok := def.EnumStrings[constants.AttributeValueID(value)]; !ok {
|
||||
if _, ok := def.EnumStrings[catvo.AttributeValueID(value)]; !ok {
|
||||
errors = append(errors, constants.QueryValueExpired)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
areValuesExpired := func(values []string, def constants.AttributeDefinition) {
|
||||
areValuesExpired := func(values []string, def catvo.AttributeDefinition) {
|
||||
if len(values) < 1 {
|
||||
errors = append(errors, constants.MissingQueryFieldValues)
|
||||
}
|
||||
|
||||
@ -4,19 +4,19 @@ import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/foomo/contentfulvalidation/constants"
|
||||
"github.com/bestbytes/catalogue/vo"
|
||||
"github.com/foomo/keel/log"
|
||||
"github.com/go-co-op/gocron"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type AttributeProviderFunc func() constants.Attributes
|
||||
type AttributeUpdateFunc func(ctx context.Context) constants.Attributes
|
||||
type AttributeProviderFunc func() vo.Attributes
|
||||
type AttributeUpdateFunc func(ctx context.Context) vo.Attributes
|
||||
|
||||
type AttributeProvider struct {
|
||||
l *zap.Logger
|
||||
ctx context.Context
|
||||
attributes constants.Attributes
|
||||
attributes vo.Attributes
|
||||
updateFunc AttributeUpdateFunc
|
||||
}
|
||||
|
||||
@ -43,6 +43,6 @@ func (ap *AttributeProvider) Init() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ap *AttributeProvider) GetAttributes() constants.Attributes {
|
||||
func (ap *AttributeProvider) GetAttributes() vo.Attributes {
|
||||
return ap.attributes
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user