Skip to content

Commit

Permalink
Merge branch 'master' into GODRIVER-3220
Browse files Browse the repository at this point in the history
  • Loading branch information
prestonvasquez committed Jun 27, 2024
2 parents eecd030 + fedd2a6 commit 1e46983
Show file tree
Hide file tree
Showing 92 changed files with 2,315 additions and 2,683 deletions.
58 changes: 58 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: "CodeQL"

on:
push:
branches: [ "v1", "cloud-*", "master", "release/*" ]
pull_request:
branches: [ "v1", "cloud-*", "master", "release/*" ]
schedule:
- cron: '36 17 * * 0'
workflow_call:
inputs:
ref:
required: true
type: string

jobs:
analyze:
name: Analyze (${{ matrix.language }})
runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-latest' }}
timeout-minutes: ${{ (matrix.language == 'swift' && 120) || 360 }}
permissions:
# required for all workflows
security-events: write

# required to fetch internal or private CodeQL packs
packages: read

# only required for workflows in private repositories
actions: read
contents: read

strategy:
fail-fast: false
matrix:
include:
- language: go
build-mode: manual

steps:
- name: Checkout repository
uses: actions/checkout@v4

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v3
with:
languages: ${{ matrix.language }}
build-mode: ${{ matrix.build-mode }}

- if: matrix.build-mode == 'manual'
shell: bash
run: |
make build
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v3
with:
category: "/language:${{matrix.language}}"
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ repos:
hooks:
- id: codespell
args: ["-L", "te,fo,fle,alo,nin,compres,wil,collone,asess,sav,ot,wll,dne,nulll,hellow"]
exclude: ^(vendor/|internal/benchmark/operation_test.go)
exclude: ^(vendor/|internal/cmd/benchmark/operation_test.go)
exclude_types: [json,yaml,pem]

- repo: https://github.com/tcort/markdown-link-check
Expand Down
21 changes: 4 additions & 17 deletions bson/array_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,11 @@ import (
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
)

// ArrayCodec is the Codec used for bsoncore.Array values.
//
// Deprecated: Use [go.mongodb.org/mongo-driver/bson.NewRegistry] to get a registry with the
// ArrayCodec registered.
type ArrayCodec struct{}

var defaultArrayCodec = NewArrayCodec()

// NewArrayCodec returns an ArrayCodec.
//
// Deprecated: Use [go.mongodb.org/mongo-driver/bson.NewRegistry] to get a registry with the
// ArrayCodec registered.
func NewArrayCodec() *ArrayCodec {
return &ArrayCodec{}
}
// arrayCodec is the Codec used for bsoncore.Array values.
type arrayCodec struct{}

// EncodeValue is the ValueEncoder for bsoncore.Array values.
func (ac *ArrayCodec) EncodeValue(_ EncodeContext, vw ValueWriter, val reflect.Value) error {
func (ac *arrayCodec) EncodeValue(_ EncodeContext, vw ValueWriter, val reflect.Value) error {
if !val.IsValid() || val.Type() != tCoreArray {
return ValueEncoderError{Name: "CoreArrayEncodeValue", Types: []reflect.Type{tCoreArray}, Received: val}
}
Expand All @@ -39,7 +26,7 @@ func (ac *ArrayCodec) EncodeValue(_ EncodeContext, vw ValueWriter, val reflect.V
}

// DecodeValue is the ValueDecoder for bsoncore.Array values.
func (ac *ArrayCodec) DecodeValue(_ DecodeContext, vr ValueReader, val reflect.Value) error {
func (ac *arrayCodec) DecodeValue(_ DecodeContext, vr ValueReader, val reflect.Value) error {
if !val.CanSet() || val.Type() != tCoreArray {
return ValueDecoderError{Name: "CoreArrayDecodeValue", Types: []reflect.Type{tCoreArray}, Received: val}
}
Expand Down
212 changes: 203 additions & 9 deletions bson/bson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"time"

"github.com/google/go-cmp/cmp"
"go.mongodb.org/mongo-driver/bson/bsonoptions"
"go.mongodb.org/mongo-driver/internal/assert"
"go.mongodb.org/mongo-driver/internal/require"
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
Expand Down Expand Up @@ -297,6 +296,202 @@ func TestD(t *testing.T) {
})
}

func TestDStringer(t *testing.T) {
got := D{{"a", 1}, {"b", 2}}.String()
want := `{"a":{"$numberInt":"1"},"b":{"$numberInt":"2"}}`
assert.Equal(t, want, got, "expected: %s, got: %s", want, got)
}

func TestMStringer(t *testing.T) {
type msg struct {
A json.RawMessage `json:"a"`
B json.RawMessage `json:"b"`
}

var res msg
got := M{"a": 1, "b": 2}.String()
err := json.Unmarshal([]byte(got), &res)
require.NoError(t, err, "Unmarshal error")

want := msg{
A: json.RawMessage(`{"$numberInt":"1"}`),
B: json.RawMessage(`{"$numberInt":"2"}`),
}

assert.Equal(t, want, res, "returned string did not unmarshal to the expected document, returned string: %s", got)
}
func TestD_MarshalJSON(t *testing.T) {
t.Parallel()

testcases := []struct {
name string
test D
expected interface{}
}{
{
"nil",
nil,
nil,
},
{
"empty",
D{},
struct{}{},
},
{
"non-empty",
D{
{"a", 42},
{"b", true},
{"c", "answer"},
{"d", nil},
{"e", 2.71828},
{"f", A{42, true, "answer", nil, 2.71828}},
{"g", D{{"foo", "bar"}}},
},
struct {
A int `json:"a"`
B bool `json:"b"`
C string `json:"c"`
D interface{} `json:"d"`
E float32 `json:"e"`
F []interface{} `json:"f"`
G map[string]interface{} `json:"g"`
}{
A: 42,
B: true,
C: "answer",
D: nil,
E: 2.71828,
F: []interface{}{42, true, "answer", nil, 2.71828},
G: map[string]interface{}{"foo": "bar"},
},
},
}
for _, tc := range testcases {
tc := tc
t.Run("json.Marshal "+tc.name, func(t *testing.T) {
t.Parallel()

got, err := json.Marshal(tc.test)
assert.NoError(t, err)
want, _ := json.Marshal(tc.expected)
assert.Equal(t, want, got)
})
}
for _, tc := range testcases {
tc := tc
t.Run("json.MarshalIndent "+tc.name, func(t *testing.T) {
t.Parallel()

got, err := json.MarshalIndent(tc.test, "<prefix>", "<indent>")
assert.NoError(t, err)
want, _ := json.MarshalIndent(tc.expected, "<prefix>", "<indent>")
assert.Equal(t, want, got)
})
}
}

func TestD_UnmarshalJSON(t *testing.T) {
t.Parallel()

t.Run("success", func(t *testing.T) {
t.Parallel()

for _, tc := range []struct {
name string
test []byte
expected D
}{
{
"nil",
[]byte(`null`),
nil,
},
{
"empty",
[]byte(`{}`),
D{},
},
{
"non-empty",
[]byte(`{"hello":"world","pi":3.142,"boolean":true,"nothing":null,"list":["hello world",3.142,false,null,{"Lorem":"ipsum"}],"document":{"foo":"bar"}}`),
D{
{"hello", "world"},
{"pi", 3.142},
{"boolean", true},
{"nothing", nil},
{"list", []interface{}{"hello world", 3.142, false, nil, D{{"Lorem", "ipsum"}}}},
{"document", D{{"foo", "bar"}}},
},
},
} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

var got D
err := json.Unmarshal(tc.test, &got)
assert.NoError(t, err)
assert.Equal(t, tc.expected, got)
})
}
})

t.Run("failure", func(t *testing.T) {
t.Parallel()

for _, tc := range []struct {
name string
test string
}{
{
"illegal",
`nil`,
},
{
"invalid",
`{"pi": 3.142ipsum}`,
},
{
"malformatted",
`{"pi", 3.142}`,
},
{
"truncated",
`{"pi": 3.142`,
},
{
"array type",
`["pi", 3.142]`,
},
{
"boolean type",
`true`,
},
} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

var a map[string]interface{}
want := json.Unmarshal([]byte(tc.test), &a)
var b D
got := json.Unmarshal([]byte(tc.test), &b)
switch w := want.(type) {
case *json.UnmarshalTypeError:
w.Type = reflect.TypeOf(b)
require.IsType(t, want, got)
g := got.(*json.UnmarshalTypeError)
assert.Equal(t, w, g)
default:
assert.Equal(t, want, got)
}
})
}
})
}

type stringerString string

func (ss stringerString) String() string {
Expand Down Expand Up @@ -349,19 +544,18 @@ func TestMapCodec(t *testing.T) {
strstr := stringerString("foo")
mapObj := map[stringerString]int{strstr: 1}
testCases := []struct {
name string
opts *bsonoptions.MapCodecOptions
key string
name string
mapCodec *mapCodec
key string
}{
{"default", bsonoptions.MapCodec(), "foo"},
{"true", bsonoptions.MapCodec().SetEncodeKeysWithStringer(true), "bar"},
{"false", bsonoptions.MapCodec().SetEncodeKeysWithStringer(false), "foo"},
{"default", &mapCodec{}, "foo"},
{"true", &mapCodec{encodeKeysWithStringer: true}, "bar"},
{"false", &mapCodec{encodeKeysWithStringer: false}, "foo"},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mapCodec := NewMapCodec(tc.opts)
mapRegistry := NewRegistry()
mapRegistry.RegisterKindEncoder(reflect.Map, mapCodec)
mapRegistry.RegisterKindEncoder(reflect.Map, tc.mapCodec)
buf := new(bytes.Buffer)
vw := NewValueWriter(buf)
enc := NewEncoder(vw)
Expand Down
2 changes: 1 addition & 1 deletion bson/bsoncodec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func ExampleValueEncoder() {
var _ ValueEncoderFunc = func(ec EncodeContext, vw ValueWriter, val reflect.Value) error {
var _ ValueEncoderFunc = func(_ EncodeContext, vw ValueWriter, val reflect.Value) error {
if val.Kind() != reflect.String {
return ValueEncoderError{Name: "StringEncodeValue", Kinds: []reflect.Kind{reflect.String}, Received: val}
}
Expand Down
Loading

0 comments on commit 1e46983

Please sign in to comment.