github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/common/convert.go (about)

     1  // This file is part of the Smart Home
     2  // Program complex distribution https://github.com/e154/smart-home
     3  // Copyright (C) 2016-2023, Filippov Alex
     4  //
     5  // This library is free software: you can redistribute it and/or
     6  // modify it under the terms of the GNU Lesser General Public
     7  // License as published by the Free Software Foundation; either
     8  // version 3 of the License, or (at your option) any later version.
     9  //
    10  // This library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13  // Library General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public
    16  // License along with this library.  If not, see
    17  // <https://www.gnu.org/licenses/>.
    18  
    19  package common
    20  
    21  import (
    22  	"math"
    23  	"time"
    24  )
    25  
    26  // String ...
    27  func String(v string) *string {
    28  	return &v
    29  }
    30  
    31  // StringValue ...
    32  func StringValue(v *string) string {
    33  	if v != nil {
    34  		return *v
    35  	}
    36  	return ""
    37  }
    38  
    39  // Int ...
    40  func Int(v int) *int {
    41  	return &v
    42  }
    43  
    44  // IntValue ...
    45  func IntValue(v *int) int {
    46  	if v != nil {
    47  		return *v
    48  	}
    49  	return 0
    50  }
    51  
    52  // Int64 ...
    53  func Int64(v int64) *int64 {
    54  	return &v
    55  }
    56  
    57  // Int64Value ...
    58  func Int64Value(v *int64) int64 {
    59  	if v != nil {
    60  		return *v
    61  	}
    62  	return 0
    63  }
    64  
    65  // Time ...
    66  func Time(v time.Time) *time.Time {
    67  	return &v
    68  }
    69  
    70  // TimeValue ...
    71  func TimeValue(v *time.Time) time.Time {
    72  	if v != nil {
    73  		return *v
    74  	}
    75  	return time.Time{}
    76  }
    77  
    78  // Rounding ...
    79  func Rounding(num float64, k uint) float64 {
    80  	p := math.Pow(10, float64(k))
    81  	return math.Floor(num*p) / p
    82  }
    83  
    84  // Rounding32 ...
    85  func Rounding32(num float64, k uint) float32 {
    86  	p := math.Pow(10, float64(k))
    87  	return float32(math.Floor(num*p) / p)
    88  }
    89  
    90  // Bool ...
    91  func Bool(v bool) *bool {
    92  	return &v
    93  }
    94  
    95  // BoolValue ...
    96  func BoolValue(v *bool) bool {
    97  	if v != nil {
    98  		return *v
    99  	}
   100  	return false
   101  }
   102  
   103  // Float32 ...
   104  func Float32(v float32) *float32 {
   105  	return &v
   106  }
   107  
   108  // Float32Value ...
   109  func Float32Value(v *float32) float32 {
   110  	if v != nil {
   111  		return *v
   112  	}
   113  	return 0
   114  }