Skip to content

Commit

Permalink
[enhance] stdlib: Implemented Bson.meta type.
Browse files Browse the repository at this point in the history
  • Loading branch information
nrs135 committed Dec 15, 2011
1 parent 97fe605 commit 9d2d0bb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
28 changes: 28 additions & 0 deletions stdlib/apis/mongo/bson.opa
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type Bson.int64 = int
type Bson.realint64 = int64
type Bson.min = void
type Bson.max = void
type Bson.meta = Bson.value

/**
* This is a type used to tell [bson_to_opa] and [opa_to_bson] to treat
Expand Down Expand Up @@ -632,6 +633,7 @@ Bson = {{
| {TyName_args=[]; TyName_ident="Bson.realint64"}
| {TyName_args=[]; TyName_ident="Bson.min"}
| {TyName_args=[]; TyName_ident="Bson.max"}
| {TyName_args=[]; TyName_ident="Bson.meta"}
| {TyName_args=[_]; TyName_ident="Bson.register"}
| {TyName_args=[_]; TyName_ident="option"}
| {TyName_args=[]; TyName_ident="bool"}
Expand Down Expand Up @@ -695,6 +697,28 @@ Bson = {{
| {TyName_args=[]; TyName_ident="Bson.realint64"} -> [H.ri64(key,(@unsafe_cast(v):Bson.realint64))]
| {TyName_args=[]; TyName_ident="Bson.min"} -> [H.min(key)]
| {TyName_args=[]; TyName_ident="Bson.max"} -> [H.max(key)]
| {TyName_args=[]; TyName_ident="Bson.meta"} ->
(match (@unsafe_cast(v):Bson.value) with
| {Double=v} -> [H.dbl(key,v)]
| {String=v} -> [H.str(key,v)]
| {Document=v} -> [H.doc(key,v)]
| {Array=v} -> [H.arr(key,v)]
| {Binary=v} -> [H.binary(key,v)]
| {ObjectID=v} -> [H.oid(key,v)]
| {Boolean=v} -> [H.bool(key,v)]
| {Date=v} -> [H.date(key,v)]
| {Null=_} -> [H.null(key)]
| {Regexp=(re,opts)} -> [H.regexp(key,(re,opts))]
| {Code=v} -> [H.code(key,v)]
| {Symbol=v} -> [H.symbol(key,v)]
| {CodeScope=v} -> [H.codescope(key,v)]
| {Int32=v} -> [H.i32(key,v)]
| {RealInt32=v} -> [H.ri32(key,v)]
| {Timestamp=(t,i)} -> [H.timestamp(key,(t,i))]
| {Int64=v} -> [H.i64(key,v)]
| {RealInt64=v} -> [H.ri64(key,v)]
| {Min=_} -> [H.min(key)]
| {Max=_} -> [H.max(key)])
| {TyName_args=[ty]; TyName_ident="Bson.register"} ->
(match (@unsafe_cast(v):Bson.register('a)) with
| {present=sv} -> opa_to_document(key,sv,ty)
Expand Down Expand Up @@ -877,6 +901,10 @@ Bson = {{
| {value={String="true"} ...} -> {some=@unsafe_cast(true)}
| {value={String="false"} ...} -> {some=@unsafe_cast(false)}
| element -> error("expected bool, got {element}",{none}))
| {TyName_args=[]; TyName_ident="Bson.meta"} ->
(match element with
| {~value ...} -> {some=@unsafe_cast(value)}
| element -> error("expected Bson.meta, got {element}",{none}))
| {TyName_args=[ty]; TyName_ident="option"} ->
(match element with
| {name="some"; ...} -> make_option(getel(element, ty))
Expand Down
23 changes: 8 additions & 15 deletions stdlib/apis/mongo/commands.opa
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,12 @@ type Mongo.explainType =
}

type Mongo.mapReduceType =
{ result : string; // TODO: read back string_or_document from doc2opa
{ result : Bson.meta;
timeMillis : int;
counts : { input : int; emit : int; output : int };
counts : { input : int; emit : int; output : int; reduce : Bson.register(int) };
ok : int
}

type Mongo.string_or_document =
{string : string}
/ {document : Bson.document}

type Mongo.mapReduceOptions = {
query: option(Bson.document);
sort: option(Bson.document);
Expand Down Expand Up @@ -732,7 +728,7 @@ MongoCommands = {{
query_opt:option(Bson.document),
sort_opt:option(Bson.document),
limit_opt:option(int),
out_opt:option(Mongo.string_or_document),
out_opt:option(Bson.meta),
keeptemp_opt:option(bool),
finalize_opt:option(string),
scope_opt:option(Bson.document),
Expand All @@ -742,10 +738,7 @@ MongoCommands = {{
(match query_opt with | {some=query} -> [H.doc("query",query)] | {none} -> []),
(match sort_opt with | {some=sort} -> [H.doc("sort",sort)] | {none} -> []),
(match limit_opt with | {some=limit} -> [H.i32("limit",limit)] | {none} -> []),
(match out_opt with
| {some={string=out}} -> [H.str("out",out)]
| {some={document=out}} -> [H.doc("out",out)]
| {none} -> []),
(match out_opt with | {some=value} -> [H.v("out",value)] | {none} -> []),
(match keeptemp_opt with | {some=keeptemp} -> [H.bool("keeptemp",keeptemp)] | {none} -> []),
(match finalize_opt with | {some=finalize} -> [H.code("finalize",finalize)] | {none} -> []),
(match scope_opt with | {some=scope} -> [H.doc("scope",scope)] | {none} -> []),
Expand Down Expand Up @@ -773,7 +766,7 @@ MongoCommands = {{
/**
* Full mapReduce but handling options with the [Mongo.mapReduceOpts] type.
**/
mapReduceOpts(m:Mongo.mongodb, map:string, reduce:string, out:Mongo.string_or_document, opts:Mongo.mapReduceOptions)
mapReduceOpts(m:Mongo.mongodb, map:string, reduce:string, out:Bson.meta, opts:Mongo.mapReduceOptions)
: Mongo.result =
mapReduce(m, map, reduce,
opts.query, opts.sort, opts.limit,
Expand All @@ -782,7 +775,7 @@ MongoCommands = {{
/**
* Simplest possible mapReduce, only define [map], [reduce] and [out].
**/
mapReduceSimple(m:Mongo.mongodb, map:string, reduce:string, out:Mongo.string_or_document): Mongo.result =
mapReduceSimple(m:Mongo.mongodb, map:string, reduce:string, out:Bson.meta): Mongo.result =
mapReduceOpts(m, map, reduce, out, mapReduceOptions)
/**
Expand All @@ -803,14 +796,14 @@ MongoCommands = {{
| {~failure} -> {~failure}
/** Same as [mapReduceOpts] but returns OPA type. **/
mapReduceOptsOpa(m:Mongo.mongodb, map:string, reduce:string, out:Mongo.string_or_document, opts:Mongo.mapReduceOptions)
mapReduceOptsOpa(m:Mongo.mongodb, map:string, reduce:string, out:Bson.meta, opts:Mongo.mapReduceOptions)
: Mongo.valresult(Mongo.mapReduceType) =
mapReduceOpa(m, map, reduce,
opts.query, opts.sort, opts.limit,
{some=out}, opts.keeptemp, opts.finalize, opts.scope, opts.jsMode, opts.verbose)
/** Same as [mapReduceSimple] but returns OPA type. **/
mapReduceSimpleOpa(m:Mongo.mongodb, map:string, reduce:string, out:Mongo.string_or_document)
mapReduceSimpleOpa(m:Mongo.mongodb, map:string, reduce:string, out:Bson.meta)
: Mongo.valresult(Mongo.mapReduceType) =
mapReduceOptsOpa(m, map, reduce, out, mapReduceOptions)
Expand Down

0 comments on commit 9d2d0bb

Please sign in to comment.