• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

looplab / eventhorizon / 12622512440

05 Jan 2025 07:58PM UTC coverage: 27.495% (-39.9%) from 67.361%
12622512440

Pull #419

github

web-flow
Merge b3c17d928 into ac3a97277
Pull Request #419: fix(ci): update to up/download-artifact v4

1769 of 6434 relevant lines covered (27.49%)

1.41 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/codec/bson/command.go
1
// Copyright (c) 2021 - The Event Horizon authors.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15
package bson
16

17
import (
18
        "context"
19
        "fmt"
20

21
        "go.mongodb.org/mongo-driver/bson"
22

23
        eh "github.com/looplab/eventhorizon"
24
)
25

26
// CommandCodec is a codec for marshaling and unmarshaling commands
27
// to and from bytes in BSON format.
28
type CommandCodec struct{}
29

30
// MarshalCommand marshals a command into bytes in BSON format.
31
func (_ CommandCodec) MarshalCommand(ctx context.Context, cmd eh.Command) ([]byte, error) {
×
32
        c := command{
×
33
                CommandType: cmd.CommandType(),
×
34
                Context:     eh.MarshalContext(ctx),
×
35
        }
×
36

×
37
        var err error
×
38
        if c.Command, err = bson.Marshal(cmd); err != nil {
×
39
                return nil, fmt.Errorf("could not marshal command data: %w", err)
×
40
        }
×
41

42
        b, err := bson.Marshal(c)
×
43
        if err != nil {
×
44
                return nil, fmt.Errorf("could not marshal command: %w", err)
×
45
        }
×
46

47
        return b, nil
×
48
}
49

50
// UnmarshalCommand unmarshals a command from bytes in BSON format.
51
func (_ CommandCodec) UnmarshalCommand(ctx context.Context, b []byte) (eh.Command, context.Context, error) {
×
52
        var c command
×
53
        if err := bson.Unmarshal(b, &c); err != nil {
×
54
                return nil, nil, fmt.Errorf("could not unmarshal command: %w", err)
×
55
        }
×
56

57
        cmd, err := eh.CreateCommand(c.CommandType)
×
58
        if err != nil {
×
59
                return nil, nil, fmt.Errorf("could not create command: %w", err)
×
60
        }
×
61

62
        if err := bson.Unmarshal(c.Command, cmd); err != nil {
×
63
                return nil, nil, fmt.Errorf("could not unmarshal command data: %w", err)
×
64
        }
×
65

66
        ctx = eh.UnmarshalContext(ctx, c.Context)
×
67

×
68
        return cmd, ctx, nil
×
69
}
70

71
// command is the internal structure used on the wire only.
72
type command struct {
73
        CommandType eh.CommandType         `bson:"command_type"`
74
        Command     bson.Raw               `bson:"command"`
75
        Context     map[string]interface{} `bson:"context"`
76
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc