code.vegaprotocol.io/vega@v0.79.0/libs/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  	"github.com/inhies/go-bytesize"
    26  )
    27  
    28  // Duration is a wrapper over an actual duration so we can represent
    29  // them as string in the toml configuration.
    30  type Duration struct {
    31  	time.Duration
    32  }
    33  
    34  // Get returns the stored duration.
    35  func (d *Duration) Get() time.Duration {
    36  	return d.Duration
    37  }
    38  
    39  // UnmarshalText unmarshal a duration from bytes.
    40  func (d *Duration) UnmarshalText(text []byte) error {
    41  	var err error
    42  	d.Duration, err = time.ParseDuration(string(text))
    43  	return err
    44  }
    45  
    46  func (d *Duration) UnmarshalFlag(s string) error {
    47  	return d.UnmarshalText([]byte(s))
    48  }
    49  
    50  // MarshalText marshal a duraton into bytes.
    51  func (d Duration) MarshalText() ([]byte, error) {
    52  	return []byte(d.String()), nil
    53  }
    54  
    55  func (d Duration) MarshalFlag() (string, error) {
    56  	bz, err := d.MarshalText()
    57  	return string(bz), err
    58  }
    59  
    60  // LogLevel is wrapper over the actual log level
    61  // so they can be specified as strings in the toml configuration.
    62  type LogLevel struct {
    63  	logging.Level
    64  }
    65  
    66  // Get return the store value.
    67  func (l *LogLevel) Get() logging.Level {
    68  	return l.Level
    69  }
    70  
    71  // UnmarshalText unmarshal a loglevel from bytes.
    72  func (l *LogLevel) UnmarshalText(text []byte) error {
    73  	var err error
    74  	l.Level, err = logging.ParseLevel(string(text))
    75  	return err
    76  }
    77  
    78  func (l *LogLevel) UnmarshalFlag(s string) error {
    79  	return l.UnmarshalText([]byte(s))
    80  }
    81  
    82  // MarshalText marshal a loglevel into bytes.
    83  func (l LogLevel) MarshalText() ([]byte, error) {
    84  	return []byte(l.String()), nil
    85  }
    86  
    87  type Bool bool
    88  
    89  func (b *Bool) UnmarshalFlag(s string) error {
    90  	if s == "true" {
    91  		*b = true
    92  	} else if s == "false" {
    93  		*b = false
    94  	} else {
    95  		return fmt.Errorf("only `true' and `false' are valid values, not `%s'", s)
    96  	}
    97  	return nil
    98  }
    99  
   100  type Base64 []byte
   101  
   102  func (b *Base64) UnmarshalFlag(s string) error {
   103  	dec, err := base64.StdEncoding.DecodeString(s)
   104  	if err != nil {
   105  		return err
   106  	}
   107  	*b = dec
   108  	return nil
   109  }
   110  
   111  func (b Base64) MarshalFlag() (string, error) {
   112  	return base64.StdEncoding.EncodeToString(b), nil
   113  }
   114  
   115  // ByteSize
   116  
   117  type ByteSize bytesize.ByteSize
   118  
   119  func (b *ByteSize) UnmarshalFlag(s string) error {
   120  	bs, err := bytesize.Parse(s)
   121  	if err != nil {
   122  		return err
   123  	}
   124  	*b = ByteSize(bs)
   125  	return nil
   126  }
   127  
   128  func (b ByteSize) MarshalFlag() (string, error) {
   129  	return bytesize.ByteSize(b).String(), nil
   130  }