-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Move field id off of schema (#2336)
## Relevant issue(s) Resolves #2334 ## Description Moves field id off of schema and onto collection. It is now a local property and does not need to be the same across multiple nodes. The new `Fields` property on `CollectionDescription` will be where other local field stuff is move too from the schema (relation_name, descriptions, secondary relation fields). This change is a prerequisite of allowing fields to be deleted and schema branching (see #2333 for more info on that if currious)
- Loading branch information
1 parent
9534113
commit db75564
Showing
89 changed files
with
782 additions
and
745 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// Copyright 2024 Democratized Data Foundation | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package client | ||
|
||
// CollectionDefinition contains the metadata defining what a Collection is. | ||
// | ||
// The definition types ([CollectionDefinition], [FieldDefinition]) are read-only types returned | ||
// from various functions as a convienient means to access the computated convergence of schema | ||
// and collection descriptions. | ||
type CollectionDefinition struct { | ||
// Description returns the CollectionDescription of this Collection. | ||
Description CollectionDescription `json:"description"` | ||
// Schema returns the SchemaDescription used to define this Collection. | ||
Schema SchemaDescription `json:"schema"` | ||
} | ||
|
||
// GetFieldByName returns the field for the given field name. If such a field is found it | ||
// will return it and true, if it is not found it will return false. | ||
func (def CollectionDefinition) GetFieldByName(fieldName string) (FieldDefinition, bool) { | ||
collectionField, ok := def.Description.GetFieldByName(fieldName) | ||
if ok { | ||
schemaField, ok := def.Schema.GetFieldByName(fieldName) | ||
if ok { | ||
return NewFieldDefinition( | ||
collectionField, | ||
schemaField, | ||
), true | ||
} | ||
} | ||
return FieldDefinition{}, false | ||
} | ||
|
||
// GetFields returns the combined local and global field elements on this [CollectionDefinition] | ||
// as a single set. | ||
func (def CollectionDefinition) GetFields() []FieldDefinition { | ||
fields := []FieldDefinition{} | ||
for _, localField := range def.Description.Fields { | ||
globalField, ok := def.Schema.GetFieldByName(localField.Name) | ||
if ok { | ||
fields = append( | ||
fields, | ||
NewFieldDefinition(localField, globalField), | ||
) | ||
} | ||
} | ||
return fields | ||
} | ||
|
||
// FieldDefinition describes the combined local and global set of properties that constitutes | ||
// a field on a collection. | ||
// | ||
// It draws it's information from the [CollectionFieldDescription] on the [CollectionDescription], | ||
// and the [SchemaFieldDescription] on the [SchemaDescription]. | ||
// | ||
// It is to [CollectionFieldDescription] and [SchemaFieldDescription] what [CollectionDefinition] | ||
// is to [CollectionDescription] and [SchemaDescription]. | ||
// | ||
// The definition types ([CollectionDefinition], [FieldDefinition]) are read-only types returned | ||
// from various functions as a convienient means to access the computated convergence of schema | ||
// and collection descriptions. | ||
type FieldDefinition struct { | ||
// Name contains the name of this field. | ||
Name string | ||
|
||
// ID contains the local, internal ID of this field. | ||
ID FieldID | ||
|
||
// The data type that this field holds. | ||
// | ||
// Must contain a valid value. It is currently immutable. | ||
Kind FieldKind | ||
|
||
// Schema contains the schema name of the type this field contains if this field is | ||
// a relation field. Otherwise this will be empty. | ||
Schema string | ||
|
||
// RelationName the name of the relationship that this field represents if this field is | ||
// a relation field. Otherwise this will be empty. | ||
RelationName string | ||
|
||
// The CRDT Type of this field. If no type has been provided it will default to [LWW_REGISTER]. | ||
// | ||
// It is currently immutable. | ||
Typ CType | ||
|
||
// If true, this is the primary half of a relation, otherwise is false. | ||
IsPrimaryRelation bool | ||
} | ||
|
||
// NewFieldDefinition returns a new [FieldDefinition], combining the given local and global elements | ||
// into a single object. | ||
func NewFieldDefinition(local CollectionFieldDescription, global SchemaFieldDescription) FieldDefinition { | ||
return FieldDefinition{ | ||
Name: global.Name, | ||
ID: local.ID, | ||
Kind: global.Kind, | ||
Schema: global.Schema, | ||
RelationName: global.RelationName, | ||
Typ: global.Typ, | ||
IsPrimaryRelation: global.IsPrimaryRelation, | ||
} | ||
} | ||
|
||
// IsRelation returns true if this field is a relation. | ||
func (f FieldDefinition) IsRelation() bool { | ||
return f.RelationName != "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.