|
| 1 | +This file provides guidance to AI coding assistants when working with code in this repository. |
| 2 | + |
| 3 | +## Project Overview |
| 4 | + |
| 5 | +gomongo is a Go library that parses MongoDB shell syntax and executes commands using the native Go MongoDB driver. It uses the ANTLR-based parser from `github.com/bytebase/parser/mongodb`. |
| 6 | + |
| 7 | +## Project Structure |
| 8 | + |
| 9 | +``` |
| 10 | +gomongo/ |
| 11 | +├── client.go # Public API: Client, NewClient, Execute |
| 12 | +├── executor.go # Parse → Translate → Execute pipeline |
| 13 | +├── translator.go # Walk ANTLR parse tree, build driver operations |
| 14 | +├── helper_functions.go # Convert ObjectId(), ISODate(), etc. to BSON |
| 15 | +├── errors.go # Error types (ParseError, UnsupportedOperationError) |
| 16 | +├── executor_test.go # Integration tests with testcontainers |
| 17 | +└── go.mod |
| 18 | +``` |
| 19 | + |
| 20 | +## Development Workflow |
| 21 | + |
| 22 | +**ALWAYS follow these steps after making code changes:** |
| 23 | + |
| 24 | +1. **Format** — Run `gofmt -w` on modified files |
| 25 | +2. **Lint** — Run `golangci-lint run --allow-parallel-runners` to catch issues |
| 26 | + - Run repeatedly until there are no issues (linter has max-issues limit) |
| 27 | +3. **Auto-fix** — Use `golangci-lint run --fix --allow-parallel-runners` to fix issues automatically |
| 28 | +4. **Test** — Run `go test -v ./...` before committing |
| 29 | +5. **Build** — Run `go build ./...` to verify compilation |
| 30 | + |
| 31 | +## Build/Test Commands |
| 32 | + |
| 33 | +```bash |
| 34 | +# Build |
| 35 | +go build ./... |
| 36 | + |
| 37 | +# Run all tests |
| 38 | +go test -v ./... |
| 39 | + |
| 40 | +# Run single test |
| 41 | +go test -v -count=1 -run ^TestFunctionName$ |
| 42 | + |
| 43 | +# Run tests with race detection |
| 44 | +go test -race -v ./... |
| 45 | + |
| 46 | +# Lint |
| 47 | +golangci-lint run --allow-parallel-runners |
| 48 | + |
| 49 | +# Format |
| 50 | +gofmt -w . |
| 51 | +``` |
| 52 | + |
| 53 | +## Code Style |
| 54 | + |
| 55 | +### General |
| 56 | + |
| 57 | +- Follow [Google Go Style Guide](https://google.github.io/styleguide/go/) |
| 58 | +- Write clean, minimal code; fewer lines is better |
| 59 | +- Prioritize simplicity for effective and maintainable software |
| 60 | +- Only include comments that are essential to understanding functionality |
| 61 | + |
| 62 | +### Go |
| 63 | + |
| 64 | +- Use standard Go error handling with detailed error messages |
| 65 | +- Always use `defer` for resource cleanup like `cursor.Close()` |
| 66 | +- Avoid using `defer` inside loops — use IIFE or scope properly |
| 67 | +- Use `any` instead of `interface{}` (Go 1.18+) |
| 68 | + |
| 69 | +### Naming |
| 70 | + |
| 71 | +- Use American English |
| 72 | +- Avoid plurals like "xxxList" |
| 73 | + |
| 74 | +### Imports |
| 75 | + |
| 76 | +- Use organized imports (sorted by import path) |
| 77 | +- Group: stdlib, external, internal |
| 78 | + |
| 79 | +### Error Handling |
| 80 | + |
| 81 | +- Be explicit but concise about error cases |
| 82 | +- Wrap errors with context using `errors.Wrap()` or `fmt.Errorf("...: %w", err)` |
| 83 | + |
| 84 | +## Common Go Lint Rules |
| 85 | + |
| 86 | +- **Unused Parameters** — Prefix unused parameters with underscore (e.g., `func foo(_ *Bar)`) |
| 87 | +- **Modern Go Conventions** — Use `any` instead of `interface{}` |
| 88 | +- **Confusing Naming** — Avoid similar names that differ only by capitalization |
| 89 | +- **Identical Branches** — Don't use if-else branches that contain identical code |
| 90 | +- **Function Receivers** — Don't create unnecessary function receivers |
| 91 | +- **Export Rules** — Only export functions and types that need to be used outside the package |
| 92 | + |
| 93 | +## Testing |
| 94 | + |
| 95 | +### Unit Tests |
| 96 | + |
| 97 | +- Test translator logic with mock parse trees |
| 98 | +- Test helper function conversions (ObjectId, ISODate, etc.) |
| 99 | +- Test error message formatting |
| 100 | + |
| 101 | +### Integration Tests |
| 102 | + |
| 103 | +Use testcontainers for MongoDB: |
| 104 | + |
| 105 | +```go |
| 106 | +func TestFind(t *testing.T) { |
| 107 | + // Start MongoDB container |
| 108 | + // Insert test documents |
| 109 | + // Execute query via gomongo |
| 110 | + // Verify results |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +## Dependencies |
| 115 | + |
| 116 | +- `go.mongodb.org/mongo-driver/v2` — Official MongoDB Go driver |
| 117 | +- `github.com/bytebase/parser` — ANTLR-based MongoDB shell parser |
| 118 | +- `github.com/antlr4-go/antlr/v4` — ANTLR runtime for Go |
| 119 | + |
| 120 | +## Output Format |
| 121 | + |
| 122 | +All query results are returned as Extended JSON (Relaxed) format using `bson.MarshalExtJSONIndent()`. This ensures: |
| 123 | +- Valid JSON that can be parsed by `JSON.parse()` |
| 124 | +- Type information preserved for BSON types |
| 125 | +- Consistent output format |
| 126 | + |
| 127 | +## Pull Request Guidelines |
| 128 | + |
| 129 | +- **Description** — Clearly describe what the PR changes and why |
| 130 | +- **Testing** — Include information about how the changes were tested |
| 131 | +- **Breaking Changes** — Clearly mark any breaking API changes |
0 commit comments