module vbson

February 26, 2023 ยท View on GitHub

Contents

encode

fn encode[T](data T) !string

encode takes only struct as input and returns BSON or returns error for failed encoding.

Use attribute bsonskip to skip encoding of any field from a struct.
Use attribute bson_oid to specify a string field as mongo-style object id.
TODO: Use attribute bson:custom_name to replace field name with a custom name.

NOTE: It uses x.json2 raw encoding/decoding. So fields having json related attributes result in unexpected behaviour.

[Return to contents]

map_to_bson

fn map_to_bson(m map[string]Any) string

[Return to contents]

bson_to_json

fn bson_to_json(b_data string) !string

[Return to contents]

bson_to_map

fn bson_to_map(data string) !map[string]Any

bson_to_map takes in bson string input and returns map[string]Any as output.
It returns error if encoded data is incorrect.

[Return to contents]

json_to_bson

fn json_to_bson(j_data string) !string

[Return to contents]

map[string]Any

to_json

fn (doc map[string]Any) to_json() !string

TODO may not be correct

[Return to contents]

Any

type Any = Binary
	| Decimal128
	| JSCode
	| MaxKey
	| MinKey
	| Null
	| ObjectID
	| Regex
	| []Any
	| bool
	| f64
	| i64
	| int
	| map[string]Any
	| string
	| time.Time
	| u64

Any consists of only the types supported by bson

[Return to contents]

[]Any

to_json

fn (arr []Any) to_json() !string

[Return to contents]

Decimal128

struct Decimal128 {
	bytes []u8
}

[Return to contents]

Binary

struct Binary {
mut:
	b_type u8
	data   []u8
}

Binary is a wrapper for binary data as per specs in bsonspec.org.

[Return to contents]

MaxKey

struct MaxKey {
}

[Return to contents]

MinKey

struct MinKey {
}

[Return to contents]

Null

struct Null {
	is_null bool = true
}

Null is placeholder for null/nil values.

[Return to contents]

ObjectID

struct ObjectID {
	id string
}

ObjectID is a wrapper for mongo-style objectID.
NOTE: Object id should be only 12 bytes long.

[Return to contents]

Regex

struct Regex {
mut:
	pattern string
	options string
}

[Return to contents]

JSCode

struct JSCode {
	code string
}

[Return to contents]

Powered by vdoc.