analyzer

package
v0.0.0-...-630eda4 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2024 License: Apache-2.0, BSD-3-Clause Imports: 10 Imported by: 0

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

View Source
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

func (gen *Generator) GetType() Type

GetType returns the final inferred type, which has been inferred from all the documents that were provided to Update

func (*Generator) TypeOf

func (gen *Generator) TypeOf(v interface{}, stack []string) Type

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"})

func (*Generator) Update

func (gen *Generator) Update(m bson.M) error

Update adds a new MongoDB document to the Generator's internal state

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

func (LiteralType) Merge

func (l LiteralType) Merge(t Type, gen *Generator) Type

type MixedType

type MixedType []Type

func (MixedType) GetNonNilType

func (m MixedType) GetNonNilType() Type

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) GoType

func (m MixedType) GoType(gen *Generator) string

func (MixedType) IsNilAndOther

func (m MixedType) IsNilAndOther() bool

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]

func (MixedType) Merge

func (m MixedType) Merge(t Type, gen *Generator) Type

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

func (PrimitiveType) Merge

func (p PrimitiveType) Merge(t Type, gen *Generator) Type

type SliceType

type SliceType struct {
	Type
}

func (SliceType) GoType

func (s SliceType) GoType(gen *Generator) string

func (SliceType) Merge

func (s SliceType) Merge(t Type, gen *Generator) Type

type StructType

type StructType map[string]Type

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

func (StructType) Merge

func (s StructType) Merge(t Type, gen *Generator) Type

type Type

type Type interface {
	GoType(gen *Generator) string
	Merge(t Type, gen *Generator) Type
}

func NewArrayType

func NewArrayType(d bson.A, gen *Generator, stack []string) Type

func NewOrderedStructType

func NewOrderedStructType(d bson.D, gen *Generator, stack []string) Type

func NewStructType

func NewStructType(m bson.M, gen *Generator, stack []string) Type

Jump to

Keyboard shortcuts

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