• 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/json/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 json
16

17
import (
18
        "context"
19
        "encoding/json"
20
        "fmt"
21

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

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

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

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

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

46
        return b, nil
×
47
}
48

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

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

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

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

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

70
// command is the internal structure used on the wire only.
71
type command struct {
72
        CommandType eh.CommandType         `json:"command_type"`
73
        Command     json.RawMessage        `json:"command"`
74
        Context     map[string]interface{} `json:"context"`
75
}
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

© 2025 Coveralls, Inc