Skip to content

Commit ca70aee

Browse files
h3n4lclaude
andauthored
refactor(gomongo): return native Go types instead of JSON strings (#22)
* refactor(gomongo): return native Go types instead of JSON strings Change Result type from returning JSON strings to native Go types for better type safety and usability. Key changes: - Add types.OperationType enum in new types/ package (single source of truth) - Result.Value is now []any containing native Go types (bson.D, string, int64, bool) instead of JSON-encoded strings - Result.Operation indicates the operation type for callers to determine expected element types in Value slice Return types by operation: - Find/Aggregate/GetIndexes/GetCollectionInfos: []bson.D - FindOne/FindOneAnd*: 0 or 1 bson.D - CountDocuments/EstimatedDocumentCount: int64 - Distinct: values of various types - ShowDatabases/ShowCollections/GetCollectionNames: strings - Write operations: bson.D with operation result - Admin operations: bson.D with {ok: 1} or bool/string Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * Address PR review feedback - Optimize executeFind to append directly to []any, avoiding intermediate slice - Use checked type assertions in README examples for safer error handling - Fix misleading comment on containsDatabaseName (values are always strings) - Fix misleading comment on getDocField helper in tests - Consolidate duplicate valueToJSON test helpers into single shared function --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 3b10cff commit ca70aee

19 files changed

+765
-993
lines changed

README.md

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"log"
2424

2525
"github.com/bytebase/gomongo"
26+
"go.mongodb.org/mongo-driver/v2/bson"
2627
"go.mongodb.org/mongo-driver/v2/mongo"
2728
"go.mongodb.org/mongo-driver/v2/mongo/options"
2829
)
@@ -46,10 +47,27 @@ func main() {
4647
log.Fatal(err)
4748
}
4849

49-
// Print results (Extended JSON format)
50-
for _, row := range result.Rows {
51-
fmt.Println(row)
50+
// Result.Value contains []any - type depends on operation
51+
// For find(), each element is bson.D
52+
for _, val := range result.Value {
53+
doc, ok := val.(bson.D)
54+
if !ok {
55+
log.Printf("unexpected type %T\n", val)
56+
continue
57+
}
58+
fmt.Printf("%+v\n", doc)
5259
}
60+
61+
// For countDocuments(), single element is int64
62+
countResult, err := gc.Execute(ctx, "mydb", `db.users.countDocuments({})`)
63+
if err != nil {
64+
log.Fatal(err)
65+
}
66+
count, ok := countResult.Value[0].(int64)
67+
if !ok {
68+
log.Fatalf("unexpected type %T\n", countResult.Value[0])
69+
}
70+
fmt.Printf("Count: %d\n", count)
5371
}
5472
```
5573

@@ -74,16 +92,19 @@ result, err := gc.Execute(ctx, "mydb", `db.users.find()`, gomongo.WithMaxRows(10
7492

7593
## Output Format
7694

77-
Results are returned in Extended JSON (Relaxed) format:
95+
Results are returned as native Go types in `Result.Value` (a `[]any` slice). Use `Result.Operation` to determine the expected type:
7896

79-
```json
80-
{
81-
"_id": {"$oid": "507f1f77bcf86cd799439011"},
82-
"name": "Alice",
83-
"age": 30,
84-
"createdAt": {"$date": "2024-01-01T00:00:00Z"}
85-
}
86-
```
97+
| Operation | Value Type |
98+
|-----------|-----------|
99+
| `OpFind`, `OpAggregate`, `OpGetIndexes`, `OpGetCollectionInfos` | Each element is `bson.D` |
100+
| `OpFindOne`, `OpFindOneAnd*` | 0 or 1 element of `bson.D` |
101+
| `OpCountDocuments`, `OpEstimatedDocumentCount` | Single `int64` |
102+
| `OpDistinct` | Elements are the distinct values |
103+
| `OpShowDatabases`, `OpShowCollections`, `OpGetCollectionNames` | Each element is `string` |
104+
| `OpInsert*`, `OpUpdate*`, `OpReplace*`, `OpDelete*` | Single `bson.D` with result |
105+
| `OpCreateIndex` | Single `string` (index name) |
106+
| `OpDropIndex`, `OpDropIndexes`, `OpCreateCollection`, `OpDropDatabase`, `OpRenameCollection` | Single `bson.D` with `{ok: 1}` |
107+
| `OpDrop` | Single `bool` (true) |
87108

88109
## Command Reference
89110

@@ -191,25 +212,25 @@ Results are returned in Extended JSON (Relaxed) format:
191212

192213
*Note: `wtimeout` is parsed but ignored as it's not supported in MongoDB Go driver v2.
193214

194-
### Milestone 3: Administrative Operations (Planned)
215+
### Milestone 3: Administrative Operations
195216

196217
#### Index Management
197218

198219
| Command | Syntax | Status |
199220
|---------|--------|--------|
200-
| db.collection.createIndex() | `createIndex(keys)` | Not yet supported |
221+
| db.collection.createIndex() | `createIndex(keys, options)` | Supported |
201222
| db.collection.createIndexes() | `createIndexes(indexSpecs)` | Not yet supported |
202-
| db.collection.dropIndex() | `dropIndex(index)` | Not yet supported |
203-
| db.collection.dropIndexes() | `dropIndexes()` | Not yet supported |
223+
| db.collection.dropIndex() | `dropIndex(index)` | Supported |
224+
| db.collection.dropIndexes() | `dropIndexes()` | Supported |
204225

205226
#### Collection Management
206227

207228
| Command | Syntax | Status |
208229
|---------|--------|--------|
209-
| db.createCollection() | `db.createCollection(name)` | Not yet supported |
210-
| db.collection.drop() | `drop()` | Not yet supported |
211-
| db.collection.renameCollection() | `renameCollection(newName)` | Not yet supported |
212-
| db.dropDatabase() | `db.dropDatabase()` | Not yet supported |
230+
| db.createCollection() | `db.createCollection(name, options)` | Supported |
231+
| db.collection.drop() | `drop()` | Supported |
232+
| db.collection.renameCollection() | `renameCollection(newName, dropTarget)` | Supported |
233+
| db.dropDatabase() | `db.dropDatabase()` | Supported |
213234

214235
#### Database Information
215236

0 commit comments

Comments
 (0)