All Downloads are FREE. Search and download functionalities are using the official Maven repository.

bson.BSONWriter.kt Maven / Gradle / Ivy

There is a newer version: 0.9.27
Show newest version
package com.github.fluidsonic.baku

import org.bson.BsonWriter


fun BsonWriter.write(name: String, boolean: Boolean) {
	writeName(name)
	writeBoolean(boolean)
}


inline fun BsonWriter.write(name: String, write: BsonWriter.() -> Unit) {
	writeName(name)
	writeDocument(write = write)
}


inline fun  BsonWriter.write(name: String, document: Value, write: BsonWriter.(value: Value) -> Unit) {
	writeName(name)
	writeDocument(document = document, write = write)
}


@JvmName("writeOrSkip")
inline fun  BsonWriter.write(name: String, documentOrSkip: Value?, write: BsonWriter.(value: Value) -> Unit) {
	documentOrSkip ?: return

	write(name = name, document = documentOrSkip, write = write)
}


fun BsonWriter.write(name: String, double: Double) {
	writeName(name)
	writeDouble(double)
}


@JvmName("writeOrSkip")
fun BsonWriter.write(name: String, doubleOrSkip: Double?) {
	if (doubleOrSkip == null) {
		return
	}

	write(name = name, double = doubleOrSkip)
}


fun BsonWriter.write(name: String, int32: Int) {
	writeName(name)
	writeInt32(int32)
}


@JvmName("writeOrSkip")
fun BsonWriter.write(name: String, int32OrSkip: Int?) {
	if (int32OrSkip == null) {
		return
	}

	write(name = name, int32 = int32OrSkip)
}


fun BsonWriter.write(name: String, string: String) {
	writeName(name)
	writeString(string)
}


fun BsonWriter.write(name: String, strings: Iterable) {
	writeName(name)
	writeStrings(strings)
}


inline fun  BsonWriter.write(name: String, value: Map, writeEntry: (entry: Map.Entry) -> Unit) {
	writeName(name)
	writeDocument {
		value.entries.forEach(writeEntry)
	}
}


@JvmName("writeOrSkip")
inline fun  BsonWriter.write(name: String, valueOrSkip: Map?, writeEntry: (entry: Map.Entry) -> Unit) {
	valueOrSkip ?: return

	write(name = name, value = valueOrSkip, writeEntry = writeEntry)
}


fun BsonWriter.write(name: String, stringOrSkip: String?, skipIfEmpty: Boolean = false) {
	if (stringOrSkip == null || (skipIfEmpty && stringOrSkip.isEmpty())) {
		return
	}

	write(name = name, string = stringOrSkip)
}


inline fun BsonWriter.writeArray(name: String, write: BsonWriter.() -> Unit) {
	writeName(name)
	writeArray(write)
}


inline fun BsonWriter.writeArray(write: BsonWriter.() -> Unit) {
	writeStartArray()
	write()
	writeEndArray()
}


inline fun BsonWriter.writeDocument(write: BsonWriter.() -> Unit) {
	writeStartDocument()
	write()
	writeEndDocument()
}


inline fun  BsonWriter.writeDocument(document: Value, write: BsonWriter.(value: Value) -> Unit) {
	writeDocument {
		write(document)
	}
}


inline fun  BsonWriter.writeMap(map: Map, writeEntry: BsonWriter.(key: Key, value: Value) -> Unit) {
	writeDocument {
		for ((key, value) in map) {
			writeEntry(key, value)
		}
	}
}


fun BsonWriter.writeStrings(strings: Iterable) {
	writeStartArray()
	for (string in strings) {
		writeString(string)
	}
	writeEndArray()
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy