schema

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 18, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package schema provides utilities to build the schema tree for the entity model.

Index

Constants

This section is empty.

Variables

View Source
var EntityModelSchemaCacheInstance = newEntityModelSchemaCache()

EntityModelSchemaCacheInstance is the singleton instance of EntityModelSchemaCache.

Functions

func GetPathForField

func GetPathForField(field, parent string) string

GetPathForField returns the schema tree path for the field.

func GetSchemaNameForModel

func GetSchemaNameForModel[T any](model T) string

GetSchemaNameForModel returns the default schema name for the model.

Types

type EntityModelSchema

type EntityModelSchema struct {
	// Root is the root node of the schema tree.
	// Root node is a dummy node, it's not a real field in the model. Actual doc parsing starts from the children of root node.
	Root TreeNode

	// Nodes is a map used to quickly access the [TreeNode] by path.
	Nodes map[string]*TreeNode
}

EntityModelSchema holds the schema tree for the entity model.

func BuildSchemaForModel

func BuildSchemaForModel[T any](model T, schemaOpts schemaopt.SchemaOptions) (*EntityModelSchema, error)

BuildSchemaForModel builds the schema tree for the given model.

func (*EntityModelSchema) IsEqual

func (s *EntityModelSchema) IsEqual(c *EntityModelSchema) bool

Used only for debugging in tests. Reason: reflect.DeepEqual doesn't provide enough information about the difference. Replace reflect.DeepEqual with this function in test file and start a debugger to get more information.

type EntityModelSchemaCache

type EntityModelSchemaCache interface {
	GetSchema(schemaName string) (*EntityModelSchema, error)
	SetSchema(schemaName string, schema *EntityModelSchema)
}

EntityModelSchemaCache is the cache implementation than can hold EntityModelSchema.

It has the following to main use cases -

  1. Avoid re-computing the schema for the same entity model.
  2. Fetch the relevant schema based on the discriminator key in case of union type models to validate the bson doc fetched from MongoDB.

type EntityModelSchemaOptions

type EntityModelSchemaOptions struct {
	// contains filtered or unexported fields
}

func NewEntityModelSchemaOptions

func NewEntityModelSchemaOptions() *EntityModelSchemaOptions

func (*EntityModelSchemaOptions) SetBSONInlineParent

func (o *EntityModelSchemaOptions) SetBSONInlineParent(bsonInlineParent bool) *EntityModelSchemaOptions

func (*EntityModelSchemaOptions) SetParentBSONFields

func (o *EntityModelSchemaOptions) SetParentBSONFields(parentBSONFields []string) *EntityModelSchemaOptions

func (*EntityModelSchemaOptions) SetXIDRequired

func (o *EntityModelSchemaOptions) SetXIDRequired(xidRequired bool) *EntityModelSchemaOptions

type SchemaFieldProps

type SchemaFieldProps struct {
	// Type holds the struct field type or the underlying type in case of pointer.
	Type reflect.Kind
	// IsPointer is used to identify pointer type of fields.
	IsPointer bool
	// Transformers are the transformations that needs to be applied on the field while building the bson doc.
	Transformers []transformer.Transformer
	// Options are the schema options for the field.
	Options fieldopt.SchemaFieldOptions
}

SchemaFieldProps are the possible field properties.

type TreeNode

type TreeNode struct {
	// Path is used to identify the ancestor chain. Used for debugging purposes.
	Path string
	// BSONKey is the translated bson key.
	BSONKey string
	// Key is the struct field name. Used for debugging purposes.
	Key string
	// Props contains the field properties.
	Props SchemaFieldProps
	// Children contains the child nodes.
	// Array is used instead of map to preserve the order of fields. Fields in bson doc should always match with the schema tree order.
	Children []TreeNode
}

TreeNode is a node in the schema tree.

func GetDefaultSchemaTreeRootNode

func GetDefaultSchemaTreeRootNode() TreeNode

GetDefaultSchemaTreeRootNode returns the default root node of a schema tree.

Directories

Path Synopsis
Package fieldopt provides custom options for schema fields.
Package fieldopt provides custom options for schema fields.
Package metafield defines and provide functions on custom meta fields for the schema.
Package metafield defines and provide functions on custom meta fields for the schema.
Package transformer provides custom transformers for schema fields.
Package transformer provides custom transformers for schema fields.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL