Skip to content

Commit

Permalink
PR FIXUP - FieldKind.IsArray etc
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewSisley committed Feb 27, 2024
1 parent de3b5e8 commit 510bbd6
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 66 deletions.
25 changes: 0 additions & 25 deletions client/definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,32 +109,7 @@ func NewFieldDefinition(local CollectionFieldDescription, global SchemaFieldDesc
}
}

// IsObject returns true if this field is an object type.
func (f FieldDefinition) IsObject() bool {
return (f.Kind == FieldKind_FOREIGN_OBJECT) ||
(f.Kind == FieldKind_FOREIGN_OBJECT_ARRAY)
}

// IsObjectArray returns true if this field is an object array type.
func (f FieldDefinition) IsObjectArray() bool {
return (f.Kind == FieldKind_FOREIGN_OBJECT_ARRAY)
}

// IsRelation returns true if this field is a relation.
func (f FieldDefinition) IsRelation() bool {
return f.RelationName != ""

Check warning on line 114 in client/definitions.go

View check run for this annotation

Codecov / codecov/patch

client/definitions.go#L113-L114

Added lines #L113 - L114 were not covered by tests
}

// IsArray returns true if this field is an array type which includes inline arrays as well
// as relation arrays.
func (f FieldDefinition) IsArray() bool {
return f.Kind == FieldKind_BOOL_ARRAY ||
f.Kind == FieldKind_INT_ARRAY ||
f.Kind == FieldKind_FLOAT_ARRAY ||
f.Kind == FieldKind_STRING_ARRAY ||
f.Kind == FieldKind_FOREIGN_OBJECT_ARRAY ||
f.Kind == FieldKind_NILLABLE_BOOL_ARRAY ||
f.Kind == FieldKind_NILLABLE_INT_ARRAY ||
f.Kind == FieldKind_NILLABLE_FLOAT_ARRAY ||
f.Kind == FieldKind_NILLABLE_STRING_ARRAY
}
50 changes: 25 additions & 25 deletions client/descriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,31 @@ func (f FieldKind) String() string {
}
}

// IsObject returns true if this FieldKind is an object type.
func (f FieldKind) IsObject() bool {
return (f == FieldKind_FOREIGN_OBJECT) ||
(f == FieldKind_FOREIGN_OBJECT_ARRAY)
}

// IsObjectArray returns true if this FieldKind is an object array type.
func (f FieldKind) IsObjectArray() bool {
return (f == FieldKind_FOREIGN_OBJECT_ARRAY)
}

// IsArray returns true if this FieldKind is an array type which includes inline arrays as well
// as relation arrays.
func (f FieldKind) IsArray() bool {
return f == FieldKind_BOOL_ARRAY ||
f == FieldKind_INT_ARRAY ||
f == FieldKind_FLOAT_ARRAY ||
f == FieldKind_STRING_ARRAY ||
f == FieldKind_FOREIGN_OBJECT_ARRAY ||
f == FieldKind_NILLABLE_BOOL_ARRAY ||
f == FieldKind_NILLABLE_INT_ARRAY ||
f == FieldKind_NILLABLE_FLOAT_ARRAY ||
f == FieldKind_NILLABLE_STRING_ARRAY
}

// Note: These values are serialized and persisted in the database, avoid modifying existing values.
const (
FieldKind_None FieldKind = 0
Expand Down Expand Up @@ -343,36 +368,11 @@ type CollectionFieldDescription struct {
ID FieldID
}

// IsObject returns true if this field is an object type.
func (f SchemaFieldDescription) IsObject() bool {
return (f.Kind == FieldKind_FOREIGN_OBJECT) ||
(f.Kind == FieldKind_FOREIGN_OBJECT_ARRAY)
}

// IsObjectArray returns true if this field is an object array type.
func (f SchemaFieldDescription) IsObjectArray() bool {
return (f.Kind == FieldKind_FOREIGN_OBJECT_ARRAY)
}

// IsRelation returns true if this field is a relation.
func (f SchemaFieldDescription) IsRelation() bool {
return f.RelationName != ""
}

// IsArray returns true if this field is an array type which includes inline arrays as well
// as relation arrays.
func (f SchemaFieldDescription) IsArray() bool {
return f.Kind == FieldKind_BOOL_ARRAY ||
f.Kind == FieldKind_INT_ARRAY ||
f.Kind == FieldKind_FLOAT_ARRAY ||
f.Kind == FieldKind_STRING_ARRAY ||
f.Kind == FieldKind_FOREIGN_OBJECT_ARRAY ||
f.Kind == FieldKind_NILLABLE_BOOL_ARRAY ||
f.Kind == FieldKind_NILLABLE_INT_ARRAY ||
f.Kind == FieldKind_NILLABLE_FLOAT_ARRAY ||
f.Kind == FieldKind_NILLABLE_STRING_ARRAY
}

// IsSet returns true if the target relation type is set.
func (m RelationType) IsSet(target RelationType) bool {
return m&target > 0
Expand Down
2 changes: 1 addition & 1 deletion client/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ func (doc *Document) Set(field string, value any) error {
if !exists {
return NewErrFieldNotExist(field)
}
if fd.IsRelation() && !fd.IsObjectArray() {
if fd.IsRelation() && !fd.Kind.IsObjectArray() {
if !strings.HasSuffix(field, request.RelatedObjectID) {
field = field + request.RelatedObjectID
}
Expand Down
2 changes: 1 addition & 1 deletion db/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -1446,7 +1446,7 @@ func (c *collection) tryGetFieldKey(primaryKey core.PrimaryDataStoreKey, fieldNa
func (c *collection) tryGetSchemaFieldID(fieldName string) (uint32, bool) {
for _, field := range c.Definition().GetFields() {
if field.Name == fieldName {
if field.IsObject() || field.IsObjectArray() {
if field.Kind.IsObject() || field.Kind.IsObjectArray() {
// We do not wish to match navigational properties, only
// fields directly on the collection.
return uint32(0), false
Expand Down
2 changes: 1 addition & 1 deletion db/collection_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ func (c *collection) makeSelectLocal(filter immutable.Option[request.Filter]) (*
}

for _, fd := range c.Schema().Fields {
if fd.IsObject() {
if fd.Kind.IsObject() {
continue
}
slct.Fields = append(slct.Fields, &request.Field{
Expand Down
8 changes: 4 additions & 4 deletions planner/mapper/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ func toSelect(
// Remap all alias field names to use their internal field name mappings.
for index, groupByField := range groupByFields {
fieldDesc, ok := definition.GetFieldByName(groupByField)
if ok && fieldDesc.IsObject() && !fieldDesc.IsObjectArray() {
if ok && fieldDesc.Kind.IsObject() && !fieldDesc.Kind.IsObjectArray() {
groupByFields[index] = groupByField + request.RelatedObjectID
} else if ok && fieldDesc.IsObjectArray() {
} else if ok && fieldDesc.Kind.IsObjectArray() {
return nil, NewErrInvalidFieldToGroupBy(groupByField)
}
}
Expand Down Expand Up @@ -289,7 +289,7 @@ func resolveAggregates(
if childIsMapped {
fieldDesc, isField := def.GetFieldByName(target.hostExternalName)

if isField && !fieldDesc.IsObject() {
if isField && !fieldDesc.Kind.IsObject() {
var order *OrderBy
if target.order.HasValue() && len(target.order.Value().Conditions) > 0 {
// For inline arrays the order element will consist of just a direction
Expand Down Expand Up @@ -792,7 +792,7 @@ func getTopLevelInfo(
definition = collection.Definition()
// Map all fields from schema into the map as they are fetched automatically
for _, f := range definition.GetFields() {
if f.IsObject() {
if f.Kind.IsObject() {
// Objects are skipped, as they are not fetched by default and
// have to be requested via selects.
continue
Expand Down
2 changes: 1 addition & 1 deletion request/graphql/parser/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func parseFilterFieldsForDescriptionMap(
fields = append(fields, parsedFileds...)
default:
f, found := col.GetFieldByName(k)
if !found || f.IsObject() {
if !found || f.Kind.IsObject() {
continue
}
fields = append(fields, f)
Expand Down
2 changes: 1 addition & 1 deletion tests/gen/gen_auto.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func validateDefinitions(definitions []client.CollectionDefinition) error {
if field.Name == "" {
return NewErrIncompleteColDefinition("field name is empty")
}
if field.IsObject() {
if field.Kind.IsObject() {
if field.Schema == "" {
return NewErrIncompleteColDefinition("field schema is empty")
}
Expand Down
4 changes: 2 additions & 2 deletions tests/gen/gen_auto_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ func checkAndValidateMinMax(field *client.SchemaFieldDescription, conf *genConfi
_, hasMin := conf.props["min"]
if hasMin {
var err error
if field.IsArray() || field.Kind == client.FieldKind_NILLABLE_INT {
err = validateMinConfig[int](conf, field.IsArray())
if field.Kind.IsArray() || field.Kind == client.FieldKind_NILLABLE_INT {
err = validateMinConfig[int](conf, field.Kind.IsArray())
} else {
err = validateMinConfig[float64](conf, false)
}
Expand Down
10 changes: 5 additions & 5 deletions tests/gen/gen_auto_configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,11 @@ func (g *docsGenConfigurator) getDemandForPrimaryType(
) (typeDemand, error) {
primaryTypeDef := g.types[primaryType]
for _, field := range primaryTypeDef.Schema.Fields {
if field.IsObject() && field.Schema == secondaryType {
if field.Kind.IsObject() && field.Schema == secondaryType {
primaryDemand := typeDemand{min: secondaryDemand.min, max: secondaryDemand.max}
minPerDoc, maxPerDoc := 1, 1

if field.IsArray() {
if field.Kind.IsArray() {
fieldConf := g.config.ForField(primaryType, field.Name)
minPerDoc, maxPerDoc = getMinMaxOrDefault(fieldConf, 0, secondaryDemand.max)
// if we request min 100 of secondary docs and there can be max 5 per primary doc,
Expand Down Expand Up @@ -339,14 +339,14 @@ func (g *docsGenConfigurator) calculateDemandForSecondaryTypes(
) error {
typeDef := g.types[typeName]
for _, field := range typeDef.Schema.Fields {
if field.IsObject() && !field.IsPrimaryRelation {
if field.Kind.IsObject() && !field.IsPrimaryRelation {
primaryDocDemand := g.docsDemand[typeName]
newSecDemand := typeDemand{min: primaryDocDemand.min, max: primaryDocDemand.max}
minPerDoc, maxPerDoc := 1, 1

curSecDemand, hasSecDemand := g.docsDemand[field.Schema]

if field.IsArray() {
if field.Kind.IsArray() {
fieldConf := g.config.ForField(typeName, field.Name)
if prop, ok := fieldConf.props["min"]; ok {
minPerDoc = prop.(int)
Expand Down Expand Up @@ -418,7 +418,7 @@ func getRelationGraph(types map[string]client.CollectionDefinition) map[string][

for typeName, typeDef := range types {
for _, field := range typeDef.Schema.Fields {
if field.IsObject() {
if field.Kind.IsObject() {
if field.IsPrimaryRelation {
primaryGraph[typeName] = appendUnique(primaryGraph[typeName], field.Schema)
} else {
Expand Down

0 comments on commit 510bbd6

Please sign in to comment.