Files
zeekay ab9f9f1f8c fix(deps): revert wire rename, keep github.com/FerretDB/wire
The rebrand commit (deb28452) renamed github.com/FerretDB/wire to
github.com/hanzoai/docdb-wire, but the hanzoai/docdb-wire repo does
not exist. Reverting that rename so the build works at HEAD and so
future merges with FerretDB upstream stay clean.

Per docdb LLM convention, the wire dep stays as FerretDB/wire
upstream. Only the FerretDB project itself is rebranded to docdb.
2026-05-18 23:22:52 -07:00

115 lines
2.5 KiB
Go

// Copyright 2021 Hanzo AI Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package server
import (
"bytes"
"encoding/json"
"fmt"
"github.com/AlekSi/lazyerrors"
"github.com/FerretDB/wire/wirebson"
"go.mongodb.org/mongo-driver/v2/bson"
)
// marshalSingleJSON converts wirebson value to the standard JSON representation.
func marshalSingleJSON(v any) (json.RawMessage, error) {
var err error
switch vt := v.(type) {
case wirebson.RawDocument:
v, err = vt.DecodeDeep()
if err != nil {
return nil, lazyerrors.Error(err)
}
case wirebson.RawArray:
v, err = vt.DecodeDeep()
if err != nil {
return nil, lazyerrors.Error(err)
}
default:
// no need to decode other types
}
bv, err := wirebson.ToDriver(v)
if err != nil {
return nil, lazyerrors.Error(err)
}
var buf bytes.Buffer
err = json.NewEncoder(&buf).Encode(bv)
if err != nil {
return nil, lazyerrors.Error(err)
}
return json.RawMessage(buf.Bytes()), nil
}
// unmarshalSingleJSON takes extended JSON object and unmarshals it into the wirebson composite.
// If provided json is nil it also returns nil.
func unmarshalSingleJSON(json *json.RawMessage) (out any, err error) {
if json == nil {
return nil, nil
}
if len(*json) == 0 {
return nil, fmt.Errorf("Invalid object: %v", *json)
}
var raw any
err = bson.UnmarshalExtJSON(*json, false, &raw)
if err != nil {
return nil, err
}
t, b, err := bson.MarshalValue(raw)
if err != nil {
return nil, err
}
switch t {
case bson.TypeEmbeddedDocument:
out = wirebson.RawDocument(b)
case bson.TypeArray:
out = wirebson.RawArray(b)
case bson.TypeDouble,
bson.TypeString,
bson.TypeBinary,
bson.TypeUndefined,
bson.TypeObjectID,
bson.TypeBoolean,
bson.TypeDateTime,
bson.TypeNull,
bson.TypeRegex,
bson.TypeDBPointer,
bson.TypeJavaScript,
bson.TypeSymbol,
bson.TypeCodeWithScope,
bson.TypeInt32,
bson.TypeTimestamp,
bson.TypeInt64,
bson.TypeDecimal128,
bson.TypeMinKey,
bson.TypeMaxKey:
fallthrough
default:
panic(fmt.Errorf("Unrecognized type: %T", t))
}
return
}