mapper

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2021 License: MIT Imports: 5 Imported by: 2

Documentation

Overview

Provides utility methods to support the converting of structs to bson maps for use in various MongoDB queries/patch updates.

It is intended to be used alongside the Mongo-Go Driver

Index

Constants

This section is empty.

Variables

View Source
var (
	// By default, this package uses `bson` as the tag name
	// You can over-write this once you have wrapped your struct
	// in the mapping struct (StructToBSON) by chaining the
	// .SetTagName() call on the wrapped struct.
	DefaultTagName = "bson"
)

Functions

func ConvertStructToBSONMap

func ConvertStructToBSONMap(s interface{}, opts *MappingOpts) bson.M

ConvertStructToBSONMap wraps a struct and converts it to a BSON Map, factoring in any options passed as arguments By default, it uses the tag name `bson` on the struct fields to generate the map The mapping is recursive for any data structures contained within the struct

Example StructToBSON to be converted:

type ExampleStruct struct {
   Value1 string `bson:"myFirstValue"`
   Value2 []int `bson:"myIntSlice"`
}

The struct is first wrapped with the "StructToBSON" type to give access to the mapping functions and is then converted to a bson.M

bson.M {
   { Key: "myFirstValue", Value: "Example String" },
   { Key: "myIntSlice", Value: {1, 2, 3, 4, 5} },
}

The following tag options are factored into the parsing:

// "omitempty" - Omit if the value is the zero value
// "omitnested" - Pass the value of the struct directly as opposed to recursively mapping the struct
// "flatten" - Pull out the data from the nested struct up one level
// "string" - Use the implementation of the Stringer interface for the value
// "-" - Do not map this field

Types

type MappingOpts

type MappingOpts struct {
	// Will just return bson.M { "_id": idVal } if the "_id" tag is present in that struct,
	// if it is not present or holds a zero value it will map the struct as you would expect.
	// Setting true on this flag gives it priority over all other functionality.
	// ie. If "_id" is present, all other fields will be ignored
	//
	// This option is included in recursive calls, so if a nested struct
	// has an "_id" tag (and the top level struct didn't) then the
	// nested struct field in the bson.M will only hold the { "_id": idVal } result.
	//
	//   // Default: False
	UseIDifAvailable bool

	// Will remove any "_id" fields from your bson.M
	// Note: this will remove "_id" fields from nested data structures as well
	//
	// 	// Default: False
	RemoveID bool

	// If true, it will check all struct fields for zero type values and
	// omit any that are found regardless of any tag options, effectively it enforces
	// the behaviour of the "omitempty" tag, regardless of whether the struct field
	// has it or not
	//
	// This logic occurs after UseIDifAvailable & RemoveID
	//
	// 	// Default: False
	GenerateFilterOrPatch bool
}

MappingOpts allows the setting of options which drive the behaviour behind how the struct is parsed

type StructToBSON

type StructToBSON struct {
	TagName string
	// contains filtered or unexported fields
}

StructToBson is the wrapper for a struct that enables this package to work

func NewBSONMapperStruct

func NewBSONMapperStruct(s interface{}) *StructToBSON

NewBSONMapperStruct returns the input struct wrapped by the mapper struct along with the tag name which should be parsed in the mapping

Panics if the argument is not a struct or pointer to a struct

func (*StructToBSON) SetTagName

func (s *StructToBSON) SetTagName(tag string)

SetTagName sets the tag name to be parsed

func (*StructToBSON) ToBSONMap

func (s *StructToBSON) ToBSONMap(opts *MappingOpts) bson.M

ToBSONMap parses all struct fields and returns a bson.M { tagName: value }. If there are nested structs it calls recursively maps them as well

Jump to

Keyboard shortcuts

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