Documentation
¶
Overview ¶
Package analyzer is based on code from the mongoschema project from Facebook (archived 2018, last change 2015) BSD-licensed original code: https://github.com/facebookarchive/mongoschema See license for this file on [LICENSE]
Main changes:
- Remove all the CLI interface code, since we're just using it as a library
- Remove all the code that generates&outputs Go struct definitions
- Add support for more Mongo types, as documented on https://pkg.go.dev/go.mongodb.org/mongo-driver@v1.16.0/bson#hdr-Native_Go_Types (e.g. the original code doesn't support Regex or CodeWithScope fields)
- Add the ability to not drill into certain objects, for cases when object keys have been used as identifiers, e.g. {friends: {123: {friended_on: 2024-01-01}, 456: {friended_on: 2024-02-01}}}, where the keys are used as a sort of many-to-many link with intermediate data
Index ¶
Constants ¶
This section is empty.
Variables ¶
var NilType = LiteralType{Literal: "nil"}
Functions ¶
This section is empty.
Types ¶
type Generator ¶
type Generator struct { // StopOnFields indicates a set of fields that will not be drilled into. Use, for example, for objects with high-cardinality keys, i.e. where the key of the object is _itself_ a variable value, such as an ID StopOnFields []string // contains filtered or unexported fields }
Generator holds state about a set of documents, so they can be incrementally analyzed. Usage: create a new Generator, then repeatedly call Update passing a MongoDB document each time. When done, call GetType once to retrieve the final type inferred from all the passed documents
func (*Generator) GetType ¶
GetType returns the final inferred type, which has been inferred from all the documents that were provided to Update
func (*Generator) TypeOf ¶
TypeOf receives an arbitrary value v, taken from a MongoDB database, and returns the Type that the value maps to. The stack parameter is the current path (in the entire document) that this value is located at, for example, if the original doc is {a: {b: 1}}, it'd be TypeOf({b: 1}, {"a"}), or TypeOf(1, {"a", "b"})
type LiteralType ¶
type LiteralType struct {
Literal string
}
LiteralType is for actual values, currently only NilType
func (LiteralType) GoType ¶
func (l LiteralType) GoType(gen *Generator) string
type MixedType ¶
type MixedType []Type
func (MixedType) GetNonNilType ¶
GetNonNilType when called on a MixedType with exactly two child types, one of which is NilType, returns "the other" (i.e. the non-nil) type. If called on a type that isn't a union of NilType and something else, it returns the MixedType that it was called on.
func (MixedType) IsNilAndOther ¶
IsNilAndOther checks whether this MixedType is just a combination of a normal type and Nil, since this indicates that a field contains null on some documents and a consistent type on others. Sometimes, this pattern must be handled as if it were of the actual type (e.g. if a field contains strings or nil, it may be useful to treat it as a normal string field that sometimes happens to have nulls, rather than a field with type Union[string, Nil]
type PrimitiveType ¶
type PrimitiveType uint
const ( PrimitiveBool PrimitiveType = iota PrimitiveDouble PrimitiveInt32 PrimitiveInt64 PrimitiveDecimal PrimitiveString PrimitiveBinary PrimitiveObjectId PrimitiveRegex PrimitiveJS PrimitiveScopedCode PrimitiveSymbol PrimitiveDateTime PrimitiveTimestamp PrimitiveDBPointer PrimitiveMinKey PrimitiveMaxKey PrimitiveUndefined )
func (PrimitiveType) GoType ¶
func (p PrimitiveType) GoType(gen *Generator) string
type StructType ¶
func (StructType) GetTypeOfChild ¶
func (s StructType) GetTypeOfChild(path string) (Type, error)
GetTypeOfChild receives a period-separated path to a field, that must be reachable from this StructType, and returns the type of that child.
For example, if the receiver is StructType{nested: StructType{field: PrimitiveString}}, then GetTypeOfChild("nested.field") returns PrimitiveString (i.e. the type that is obtained by navigating first to nested and then to field)
func (StructType) GoType ¶
func (s StructType) GoType(gen *Generator) string