code.vegaprotocol.io/vega@v0.79.0/datanode/gateway/graphql/helpers.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 gql
    17  
    18  import (
    19  	"fmt"
    20  	"math"
    21  	"strconv"
    22  
    23  	"code.vegaprotocol.io/vega/datanode/vegatime"
    24  	"code.vegaprotocol.io/vega/libs/ptr"
    25  )
    26  
    27  func safeStringUint64(input string) (uint64, error) {
    28  	i, err := strconv.ParseUint(input, 10, 64)
    29  	if err != nil {
    30  		// A conversion error occurred, return the error
    31  		return 0, fmt.Errorf("invalid input string for uint64 conversion %s", input)
    32  	}
    33  	return i, nil
    34  }
    35  
    36  func safeStringInt64(input string) (int64, error) {
    37  	i, err := strconv.ParseInt(input, 10, 64)
    38  	if err != nil {
    39  		// A conversion error occurred, return the error
    40  		return 0, fmt.Errorf("invalid input string for int64 conversion %s", input)
    41  	}
    42  	return i, nil
    43  }
    44  
    45  func secondsTSToDatetime(timestampInSeconds int64) string {
    46  	return vegatime.Format(vegatime.Unix(timestampInSeconds, 0))
    47  }
    48  
    49  func nanoTSToDatetime(timestampInNanoSeconds int64) string {
    50  	return vegatime.Format(vegatime.UnixNano(timestampInNanoSeconds))
    51  }
    52  
    53  func convertVersion(version *int) (*int32, error) {
    54  	if version != nil {
    55  		if *version >= 1 && *version < math.MaxInt32 {
    56  			return ptr.From(int32(*version)), nil
    57  		}
    58  		return nil, fmt.Errorf("invalid version value %d", *version)
    59  	}
    60  	return nil, nil
    61  }