fieldopt

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: 6 Imported by: 0

Documentation

Overview

Package fieldopt provides custom options for schema fields.

Index

Constants

This section is empty.

Variables

View Source
var DefaultValueOption = newDefaultValueOption()

DefaultValueOption provides the default value for a field. The value of this option is used when the field is not present in the input document. This option is applicable only for fields that are not of type struct. Defaults to nil for all fields.

View Source
var RequiredOption = newRequiredOption()

RequiredOption defines if a field is required or not. The option can be invalidated using `omitempty` property of `bson` tag. Defaults to true for all fields.

View Source
var XIDOption = newXIDOption()

XIDOption defines if `_id` field needs to be added in a object. This option is applicable for fields holding structs only. Defaults to true for struct fields.

Functions

This section is empty.

Types

type FieldOption

type FieldOption interface {
	// GetOptName returns the name of the schema option. This name is used to identify the unique option.
	// NOTE: Make sure to return the same name as the name of the field in [SchemaFieldOptions] struct.
	GetOptName() string
	// GetBSONTagName returns the bson tag name for the option. This is used to identify the option and its flags in the bson tag.
	GetBSONTagName() string
	// IsApplicable returns true if the option is applicable for the field.
	IsApplicable(field reflect.StructField) bool
	// GetDefaultValue returns the default value for the field (if available).
	GetDefaultValue(field reflect.StructField) interface{}
	// GetValue returns the provided value for the field.
	GetValue(field reflect.StructField) (interface{}, error)
}

FieldOption is the interface that needs to be implemented by all SchemaFieldOptions.

type FieldOptionTag added in v0.2.0

type FieldOptionTag string

FieldOptionTag is the BSON tag by which the properties of a field option is configured.

const (
	FieldOptionTagRequired FieldOptionTag = "bson"
	FieldOptionTagXID      FieldOptionTag = "mgoID"
	FieldOptionTagDefault  FieldOptionTag = "mgoDefault"
)

type SchemaFieldOptions

type SchemaFieldOptions struct {
	// Required suggests whether the field is required or not. [FIELD_LEVEL]
	// Defaults to true. Can be identified using omitempty bson flag.
	Required bool
	// XID suggests whether "_id" field needs to be added in the bson doc for the following object type field. [STRUCT_LEVEL]
	// Defaults to true. This option is applicable for fields holding structs only.
	XID bool
	// Default is the default value for the field. [FIELD_LEVEL]
	// Defaults to nil. Will be populated using reflect and will be of the same type as Type in SchemaFieldProps.
	Default interface{}
	// not implemented yet
	Select bool
}

SchemaFieldOptions are custom schema options available for struct fields. These options either modifies the schema based on the field or adds validations to the field.

func GetSchemaOptionsForField

func GetSchemaOptionsForField(field reflect.StructField) (SchemaFieldOptions, error)

GetSchemaOptionsForField returns all the applicable schema field options for the provided field.

Jump to

Keyboard shortcuts

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