Hi
Note: I didn’t run below codes with Meteor, so, please, let us know about any results.
Short solution, but hackish:
native class Object
class Pair<K, V>(val first: K, val second: V)
fun <K, V> K.to(v: V) = Pair(this, v)
fun json<V>(vararg values: Pair<String, V>): Any {
[native] val o = Object() as Map<String, Any?>
for (v in values) {
o[v.first] = v.second
}
return o
}
native
public object Meteor {
public var isClient : Boolean = false
public var isServer : Boolean = false
public var release : String = “”
native(“Meteor.Collection”)
public class Collection(name : String) {
native(“Meteor.Collection.Cursor”)
public class Cursor {
}
fun find(selector : Any, options: Any) : Cursor = noImpl
}
}
fun test() {
Meteor.Collection(“Foo”).find(json<Any>(), json(“sort” to json(“foo” to “asc”, “bar” to “desc”)))
Meteor.Collection(“Foo”).find(Object(), json(“sort” to array(array(“foo”, “asc”), array(“bar”, “asc”), “baz”)))
}
Next, more type safe version:
native(“Object”) class Selector
native(“Object”) class SortOptions
native(“Object”) class Options {
var sort: SortOptions = noImpl
var skip: Number = noImpl
// …
}
fun options(
sort: SortOptions? = null,
skip: Int? = null
): Options {
val o = Options()
if (sort != null) o.sort = sort
if (skip != null) o.skip = skip
return o
}
native class FieldSortOption
val String.ASC: FieldSortOption
get() = array(this, “asc”) as FieldSortOption
val String.DESC: FieldSortOption
get() = array(this, “asc”) as FieldSortOption
fun sortOptions(vararg options: FieldSortOption) = options as SortOptions
native
public object Meteor {
public var isClient : Boolean = false
public var isServer : Boolean = false
public var release : String = “”
native(“Meteor.Collection”)
public class Collection(name : String) {
native(“Meteor.Collection.Cursor”)
public class Cursor {
}
fun find(selector : Selector, options: Options) : Cursor = noImpl
}
}
fun test() {
Meteor.Collection(“Foo”).find(Selector(), options(sort = sortOptions(“foo”.ASC, “bar”.DESC)))
}
And mixed version:
native class Object
class Pair<K, V>(val first: K, val second: V)
fun <K, V> K.to(v: V) = Pair(this, v)
fun json<V>(vararg values: Pair<String, V>): Any {
[native] val o = Object() as Map<String, Any?>
for (v in values) {
o[v.first] = v.second
}
return o
}
native(“Object”) class Selector
native(“Object”) class Options {
var sort: Any = noImpl
var skip: Number = noImpl
// …
}
native
public object Meteor {
public var isClient : Boolean = false
public var isServer : Boolean = false
public var release : String = “”
native(“Meteor.Collection”)
public class Collection(name : String) {
native(“Meteor.Collection.Cursor”)
public class Cursor {
}
fun find(selector : Selector, options: Options) : Cursor = noImpl
}
}
fun test() {
val o = Options()
o.sort = json(“foo” to “asc”, “bar” to “desc”)
o.sort = array(array(“foo”, “asc”), array(“bar”, “asc”), “baz”)
Meteor.Collection(“Foo”).find(Selector(), o)
}