...
1 package pgproto3
2
3 import (
4 "encoding/hex"
5 "encoding/json"
6 )
7
8 type SASLResponse struct {
9 Data []byte
10 }
11
12
13 func (*SASLResponse) Frontend() {}
14
15
16
17 func (dst *SASLResponse) Decode(src []byte) error {
18 *dst = SASLResponse{Data: src}
19 return nil
20 }
21
22
23 func (src *SASLResponse) Encode(dst []byte) ([]byte, error) {
24 dst, sp := beginMessage(dst, 'p')
25 dst = append(dst, src.Data...)
26 return finishMessage(dst, sp)
27 }
28
29
30 func (src SASLResponse) MarshalJSON() ([]byte, error) {
31 return json.Marshal(struct {
32 Type string
33 Data string
34 }{
35 Type: "SASLResponse",
36 Data: string(src.Data),
37 })
38 }
39
40
41 func (dst *SASLResponse) UnmarshalJSON(data []byte) error {
42 var msg struct {
43 Data string
44 }
45 if err := json.Unmarshal(data, &msg); err != nil {
46 return err
47 }
48 if msg.Data != "" {
49 decoded, err := hex.DecodeString(msg.Data)
50 if err != nil {
51 return err
52 }
53 dst.Data = decoded
54 }
55 return nil
56 }
57
View as plain text