code.vegaprotocol.io/vega@v0.79.0/datanode/config/encoding/encoding.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package encoding 17 18 import ( 19 "encoding/base64" 20 "fmt" 21 "time" 22 23 "code.vegaprotocol.io/vega/logging" 24 ) 25 26 // Duration is a wrapper over an actual duration so we can represent 27 // them as string in the toml configuration. 28 type Duration struct { 29 time.Duration 30 } 31 32 // Get returns the stored duration. 33 func (d *Duration) Get() time.Duration { 34 return d.Duration 35 } 36 37 // UnmarshalText unmarshal a duration from bytes. 38 func (d *Duration) UnmarshalText(text []byte) error { 39 var err error 40 d.Duration, err = time.ParseDuration(string(text)) 41 return err 42 } 43 44 func (d *Duration) UnmarshalFlag(s string) error { 45 return d.UnmarshalText([]byte(s)) 46 } 47 48 // MarshalText marshal a duraton into bytes. 49 func (d Duration) MarshalText() ([]byte, error) { 50 return []byte(d.String()), nil 51 } 52 53 func (d Duration) MarshalFlag() (string, error) { 54 bz, err := d.MarshalText() 55 return string(bz), err 56 } 57 58 // LogLevel is wrapper over the actual log level 59 // so they can be specified as strings in the toml configuration. 60 type LogLevel struct { 61 logging.Level 62 } 63 64 // Get return the store value. 65 func (l *LogLevel) Get() logging.Level { 66 return l.Level 67 } 68 69 // UnmarshalText unmarshal a loglevel from bytes. 70 func (l *LogLevel) UnmarshalText(text []byte) error { 71 var err error 72 l.Level, err = logging.ParseLevel(string(text)) 73 return err 74 } 75 76 func (l *LogLevel) UnmarshalFlag(s string) error { 77 return l.UnmarshalText([]byte(s)) 78 } 79 80 // MarshalText marshal a loglevel into bytes. 81 func (l LogLevel) MarshalText() ([]byte, error) { 82 return []byte(l.String()), nil 83 } 84 85 type Bool bool 86 87 func (b *Bool) UnmarshalFlag(s string) error { 88 if s == "true" { 89 *b = true 90 } else if s == "false" { 91 *b = false 92 } else { 93 return fmt.Errorf("only `true' and `false' are valid values, not `%s'", s) 94 } 95 return nil 96 } 97 98 type Base64 []byte 99 100 func (b *Base64) UnmarshalFlag(s string) error { 101 dec, err := base64.StdEncoding.DecodeString(s) 102 if err != nil { 103 return err 104 } 105 *b = dec 106 return nil 107 } 108 109 func (b Base64) MarshalFlag() (string, error) { 110 return base64.StdEncoding.EncodeToString(b), nil 111 }