code.vegaprotocol.io/vega@v0.79.0/libs/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 "time" 20 21 "go.uber.org/zap/zapcore" 22 ) 23 24 // Duration is a wrapper over an actual duration, so we can represent 25 // them as string in the toml configuration. 26 type Duration struct { 27 time.Duration 28 } 29 30 // Get returns the stored duration. 31 func (d *Duration) Get() time.Duration { 32 return d.Duration 33 } 34 35 // UnmarshalText unmarshal a duration from bytes. 36 func (d *Duration) UnmarshalText(text []byte) error { 37 var err error 38 d.Duration, err = time.ParseDuration(string(text)) 39 return err 40 } 41 42 // MarshalText marshal a duration into bytes. 43 func (d Duration) MarshalText() ([]byte, error) { 44 return []byte(d.String()), nil 45 } 46 47 // LogLevel is wrapper over the actual log level, 48 // so they can be specified as strings in the toml configuration. 49 type LogLevel struct { 50 zapcore.Level 51 } 52 53 // Get return the store value. 54 func (l *LogLevel) Get() zapcore.Level { 55 return l.Level 56 } 57 58 // UnmarshalText unmarshal a loglevel from bytes. 59 func (l *LogLevel) UnmarshalText(text []byte) error { 60 return l.Level.UnmarshalText(text) 61 } 62 63 // MarshalText marshal a loglevel into bytes. 64 func (l LogLevel) MarshalText() ([]byte, error) { 65 return []byte(l.String()), nil 66 }