github.com/matrixorigin/matrixone@v1.2.0/pkg/util/toml/types.go (about)

     1  // Copyright 2021 - 2022 Matrix Origin
     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 toml
    16  
    17  import (
    18  	"time"
    19  
    20  	"github.com/docker/go-units"
    21  )
    22  
    23  // Duration is a wrapper of time.Duration for TOML
    24  type Duration struct {
    25  	time.Duration
    26  }
    27  
    28  // UnmarshalText parses a TOML string into the duration.
    29  func (d *Duration) UnmarshalText(text []byte) error {
    30  	var err error
    31  	d.Duration, err = time.ParseDuration(string(text))
    32  	return err
    33  }
    34  
    35  // MarshalText returns the duration as a JSON string.
    36  func (d Duration) MarshalText() ([]byte, error) {
    37  	return []byte(d.String()), nil
    38  }
    39  
    40  // ByteSize is a retype uint64 for TOML.
    41  type ByteSize uint64
    42  
    43  // UnmarshalText parses a Toml string into the byte size.
    44  func (b *ByteSize) UnmarshalText(text []byte) error {
    45  	v, err := units.RAMInBytes(string(text))
    46  	if err != nil {
    47  		return err
    48  	}
    49  	*b = ByteSize(v)
    50  	return nil
    51  }