File: unmarshal.go

package info (click to toggle)
golang-github-centrifugal-centrifuge 0.15.0%2Bgit20210306.f435ba2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,612 kB
  • sloc: javascript: 102; makefile: 2
file content (83 lines) | stat: -rw-r--r-- 2,039 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package controlproto

import "github.com/centrifugal/centrifuge/internal/controlpb"

// Decoder ...
type Decoder interface {
	DecodeCommand([]byte) (*controlpb.Command, error)
	DecodeNode([]byte) (*controlpb.Node, error)
	DecodeUnsubscribe([]byte) (*controlpb.Unsubscribe, error)
	DecodeDisconnect([]byte) (*controlpb.Disconnect, error)
	DecodeSurveyRequest([]byte) (*controlpb.SurveyRequest, error)
	DecodeSurveyResponse([]byte) (*controlpb.SurveyResponse, error)
}

var _ Decoder = (*ProtobufDecoder)(nil)

// ProtobufDecoder ...
type ProtobufDecoder struct{}

// NewProtobufDecoder ...
func NewProtobufDecoder() *ProtobufDecoder {
	return &ProtobufDecoder{}
}

// DecodeCommand ...
func (e *ProtobufDecoder) DecodeCommand(data []byte) (*controlpb.Command, error) {
	var cmd controlpb.Command
	err := cmd.Unmarshal(data)
	if err != nil {
		return nil, err
	}
	return &cmd, nil
}

// DecodeNode ...
func (e *ProtobufDecoder) DecodeNode(data []byte) (*controlpb.Node, error) {
	var cmd controlpb.Node
	err := cmd.Unmarshal(data)
	if err != nil {
		return nil, err
	}
	return &cmd, nil
}

// DecodeUnsubscribe ...
func (e *ProtobufDecoder) DecodeUnsubscribe(data []byte) (*controlpb.Unsubscribe, error) {
	var cmd controlpb.Unsubscribe
	err := cmd.Unmarshal(data)
	if err != nil {
		return nil, err
	}
	return &cmd, nil
}

// DecodeDisconnect ...
func (e *ProtobufDecoder) DecodeDisconnect(data []byte) (*controlpb.Disconnect, error) {
	var cmd controlpb.Disconnect
	err := cmd.Unmarshal(data)
	if err != nil {
		return nil, err
	}
	return &cmd, nil
}

// DecodeSurveyRequest ...
func (e *ProtobufDecoder) DecodeSurveyRequest(data []byte) (*controlpb.SurveyRequest, error) {
	var cmd controlpb.SurveyRequest
	err := cmd.Unmarshal(data)
	if err != nil {
		return nil, err
	}
	return &cmd, nil
}

// DecodeSurveyResponse ...
func (e *ProtobufDecoder) DecodeSurveyResponse(data []byte) (*controlpb.SurveyResponse, error) {
	var cmd controlpb.SurveyResponse
	err := cmd.Unmarshal(data)
	if err != nil {
		return nil, err
	}
	return &cmd, nil
}