|
| 1 | +// Copyright 2021 dfuse Platform Inc. |
| 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 print |
| 16 | + |
| 17 | +import ( |
| 18 | + "encoding/base64" |
| 19 | + "encoding/hex" |
| 20 | + "fmt" |
| 21 | + "io" |
| 22 | + |
| 23 | + "github.com/mr-tron/base58" |
| 24 | + pbbstream "github.com/streamingfast/bstream/pb/sf/bstream/v1" |
| 25 | + fcproto "github.com/streamingfast/firehose-core/proto" |
| 26 | + pbfirehose "github.com/streamingfast/pbgo/sf/firehose/v2" |
| 27 | + "google.golang.org/protobuf/proto" |
| 28 | +) |
| 29 | + |
| 30 | +var _ OutputPrinter = (*BytesOutputPrinter)(nil) |
| 31 | + |
| 32 | +type BytesOutputPrinter struct { |
| 33 | + bytesEncoding string |
| 34 | + registry *fcproto.Registry |
| 35 | +} |
| 36 | + |
| 37 | +func NewBytesOutputPrinter(bytesEncoding string, registry *fcproto.Registry) *BytesOutputPrinter { |
| 38 | + return &BytesOutputPrinter{ |
| 39 | + bytesEncoding: bytesEncoding, |
| 40 | + registry: registry, |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func (p *BytesOutputPrinter) PrintTo(input any, out io.Writer) error { |
| 45 | + if pbblock, ok := input.(*pbbstream.Block); ok { |
| 46 | + return p.printBytes(pbblock.Payload.Value, out) |
| 47 | + } |
| 48 | + |
| 49 | + if v, ok := input.(*pbfirehose.Response); ok { |
| 50 | + return p.printBytes(v.Block.Value, out) |
| 51 | + } |
| 52 | + |
| 53 | + if v, ok := input.(*pbfirehose.SingleBlockResponse); ok { |
| 54 | + return p.printBytes(v.Block.Value, out) |
| 55 | + } |
| 56 | + |
| 57 | + if v, ok := input.(proto.Message); ok { |
| 58 | + data, err := proto.Marshal(v) |
| 59 | + if err != nil { |
| 60 | + return fmt.Errorf("unable to marshal proto message: %w", err) |
| 61 | + } |
| 62 | + |
| 63 | + return p.printBytes(data, out) |
| 64 | + } |
| 65 | + |
| 66 | + return fmt.Errorf("unsupported type %T", input) |
| 67 | +} |
| 68 | + |
| 69 | +var base64Encoding = base64.StdEncoding |
| 70 | + |
| 71 | +func (p *BytesOutputPrinter) printBytes(data []byte, out io.Writer) error { |
| 72 | + var err error |
| 73 | + switch p.bytesEncoding { |
| 74 | + case "hex": |
| 75 | + err = p.printBytesHex(data, out) |
| 76 | + case "base58": |
| 77 | + err = p.printBytesBase58(data, out) |
| 78 | + case "base64": |
| 79 | + err = p.printBytesBase64(data, out) |
| 80 | + default: |
| 81 | + return fmt.Errorf("unsupported bytes encoding %q", p.bytesEncoding) |
| 82 | + } |
| 83 | + if err != nil { |
| 84 | + return fmt.Errorf("unable to print bytes: %w", err) |
| 85 | + } |
| 86 | + |
| 87 | + return writeStringToWriter(out, "") |
| 88 | +} |
| 89 | + |
| 90 | +func (p *BytesOutputPrinter) printBytesHex(data []byte, out io.Writer) error { |
| 91 | + encoder := hex.NewEncoder(out) |
| 92 | + _, err := encoder.Write(data) |
| 93 | + return err |
| 94 | +} |
| 95 | + |
| 96 | +func (p *BytesOutputPrinter) printBytesBase58(data []byte, out io.Writer) error { |
| 97 | + return writeStringToWriter(out, base58.Encode(data)) |
| 98 | +} |
| 99 | + |
| 100 | +func (p *BytesOutputPrinter) printBytesBase64(data []byte, out io.Writer) error { |
| 101 | + encoder := base64.NewEncoder(base64Encoding, out) |
| 102 | + // This flushes the base64 encoder but doesn't close the underlying writer, which is |
| 103 | + // what we want here |
| 104 | + defer encoder.Close() |
| 105 | + |
| 106 | + _, err := encoder.Write(data) |
| 107 | + return err |
| 108 | +} |
0 commit comments