github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/datacodec/conversions.go (about)

     1  // Copyright 2021 DataStax
     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 datacodec
    16  
    17  import (
    18  	"errors"
    19  	"math"
    20  	"math/big"
    21  	"strconv"
    22  	"time"
    23  )
    24  
    25  func int64ToInt(val int64, intSize int) (int, error) {
    26  	if intSize == 32 && (val < int64(math.MinInt32) || val > int64(math.MaxInt32)) {
    27  		return 0, errValueOutOfRange(val)
    28  	} else {
    29  		return int(val), nil
    30  	}
    31  }
    32  
    33  func int64ToInt32(val int64) (int32, error) {
    34  	if val < math.MinInt32 || val > math.MaxInt32 {
    35  		return 0, errValueOutOfRange(val)
    36  	} else {
    37  		return int32(val), nil
    38  	}
    39  }
    40  
    41  func int64ToInt16(val int64) (int16, error) {
    42  	if val < math.MinInt16 || val > math.MaxInt16 {
    43  		return 0, errValueOutOfRange(val)
    44  	} else {
    45  		return int16(val), nil
    46  	}
    47  }
    48  
    49  func int64ToInt8(val int64) (int8, error) {
    50  	if val < math.MinInt8 || val > math.MaxInt8 {
    51  		return 0, errValueOutOfRange(val)
    52  	} else {
    53  		return int8(val), nil
    54  	}
    55  }
    56  
    57  func int64ToUint64(val int64) (uint64, error) {
    58  	if val < 0 {
    59  		return 0, errValueOutOfRange(val)
    60  	} else {
    61  		return uint64(val), nil
    62  	}
    63  }
    64  
    65  func int64ToUint(val int64, intSize int) (uint, error) {
    66  	if val < 0 || (intSize == 32 && uint64(val) > uint64(math.MaxUint32)) {
    67  		return 0, errValueOutOfRange(val)
    68  	} else {
    69  		return uint(val), nil
    70  	}
    71  }
    72  
    73  func int64ToUint32(val int64) (uint32, error) {
    74  	if val < 0 || val > math.MaxUint32 {
    75  		return 0, errValueOutOfRange(val)
    76  	} else {
    77  		return uint32(val), nil
    78  	}
    79  }
    80  
    81  func int64ToUint16(val int64) (uint16, error) {
    82  	if val < 0 || val > math.MaxUint16 {
    83  		return 0, errValueOutOfRange(val)
    84  	} else {
    85  		return uint16(val), nil
    86  	}
    87  }
    88  
    89  func int64ToUint8(val int64) (uint8, error) {
    90  	if val < 0 || val > math.MaxUint8 {
    91  		return 0, errValueOutOfRange(val)
    92  	} else {
    93  		return uint8(val), nil
    94  	}
    95  }
    96  
    97  func intToInt32(val int) (int32, error) {
    98  	if val < math.MinInt32 || val > math.MaxInt32 {
    99  		return 0, errValueOutOfRange(val)
   100  	} else {
   101  		return int32(val), nil
   102  	}
   103  }
   104  
   105  func intToInt16(val int) (int16, error) {
   106  	if val < math.MinInt16 || val > math.MaxInt16 {
   107  		return 0, errValueOutOfRange(val)
   108  	} else {
   109  		return int16(val), nil
   110  	}
   111  }
   112  
   113  func intToInt8(val int) (int8, error) {
   114  	if val < math.MinInt8 || val > math.MaxInt8 {
   115  		return 0, errValueOutOfRange(val)
   116  	} else {
   117  		return int8(val), nil
   118  	}
   119  }
   120  
   121  func int32ToInt16(val int32) (int16, error) {
   122  	if val < math.MinInt16 || val > math.MaxInt16 {
   123  		return 0, errValueOutOfRange(val)
   124  	} else {
   125  		return int16(val), nil
   126  	}
   127  }
   128  
   129  func int32ToInt8(val int32) (int8, error) {
   130  	if val < math.MinInt8 || val > math.MaxInt8 {
   131  		return 0, errValueOutOfRange(val)
   132  	} else {
   133  		return int8(val), nil
   134  	}
   135  }
   136  
   137  func int32ToUint64(val int32) (uint64, error) {
   138  	if val < 0 {
   139  		return 0, errValueOutOfRange(val)
   140  	} else {
   141  		return uint64(val), nil
   142  	}
   143  }
   144  
   145  func int32ToUint(val int32) (uint, error) {
   146  	if val < 0 {
   147  		return 0, errValueOutOfRange(val)
   148  	} else {
   149  		return uint(val), nil
   150  	}
   151  }
   152  
   153  func int32ToUint32(val int32) (uint32, error) {
   154  	if val < 0 {
   155  		return 0, errValueOutOfRange(val)
   156  	} else {
   157  		return uint32(val), nil
   158  	}
   159  }
   160  
   161  func int32ToUint16(val int32) (uint16, error) {
   162  	if val < 0 || val > math.MaxUint16 {
   163  		return 0, errValueOutOfRange(val)
   164  	} else {
   165  		return uint16(val), nil
   166  	}
   167  }
   168  
   169  func int32ToUint8(val int32) (uint8, error) {
   170  	if val < 0 || val > math.MaxUint8 {
   171  		return 0, errValueOutOfRange(val)
   172  	} else {
   173  		return uint8(val), nil
   174  	}
   175  }
   176  
   177  func int16ToInt8(val int16) (int8, error) {
   178  	if val < math.MinInt8 || val > math.MaxInt8 {
   179  		return 0, errValueOutOfRange(val)
   180  	} else {
   181  		return int8(val), nil
   182  	}
   183  }
   184  
   185  func int16ToUint64(val int16) (uint64, error) {
   186  	if val < 0 {
   187  		return 0, errValueOutOfRange(val)
   188  	} else {
   189  		return uint64(val), nil
   190  	}
   191  }
   192  
   193  func int16ToUint(val int16) (uint, error) {
   194  	if val < 0 {
   195  		return 0, errValueOutOfRange(val)
   196  	} else {
   197  		return uint(val), nil
   198  	}
   199  }
   200  
   201  func int16ToUint32(val int16) (uint32, error) {
   202  	if val < 0 {
   203  		return 0, errValueOutOfRange(val)
   204  	} else {
   205  		return uint32(val), nil
   206  	}
   207  }
   208  
   209  func int16ToUint16(val int16) (uint16, error) {
   210  	if val < 0 {
   211  		return 0, errValueOutOfRange(val)
   212  	} else {
   213  		return uint16(val), nil
   214  	}
   215  }
   216  
   217  func int16ToUint8(val int16) (uint8, error) {
   218  	if val < 0 || val > math.MaxUint8 {
   219  		return 0, errValueOutOfRange(val)
   220  	} else {
   221  		return uint8(val), nil
   222  	}
   223  }
   224  func int8ToUint64(val int8) (uint64, error) {
   225  	if val < 0 {
   226  		return 0, errValueOutOfRange(val)
   227  	} else {
   228  		return uint64(val), nil
   229  	}
   230  }
   231  
   232  func int8ToUint(val int8) (uint, error) {
   233  	if val < 0 {
   234  		return 0, errValueOutOfRange(val)
   235  	} else {
   236  		return uint(val), nil
   237  	}
   238  }
   239  
   240  func int8ToUint32(val int8) (uint32, error) {
   241  	if val < 0 {
   242  		return 0, errValueOutOfRange(val)
   243  	} else {
   244  		return uint32(val), nil
   245  	}
   246  }
   247  
   248  func int8ToUint16(val int8) (uint16, error) {
   249  	if val < 0 {
   250  		return 0, errValueOutOfRange(val)
   251  	} else {
   252  		return uint16(val), nil
   253  	}
   254  }
   255  
   256  func int8ToUint8(val int8) (uint8, error) {
   257  	if val < 0 {
   258  		return 0, errValueOutOfRange(val)
   259  	} else {
   260  		return uint8(val), nil
   261  	}
   262  }
   263  
   264  func uint64ToInt64(val uint64) (int64, error) {
   265  	if val > math.MaxInt64 {
   266  		return 0, errValueOutOfRange(val)
   267  	} else {
   268  		return int64(val), nil
   269  	}
   270  }
   271  
   272  func uint64ToInt32(val uint64) (int32, error) {
   273  	if val > math.MaxInt32 {
   274  		return 0, errValueOutOfRange(val)
   275  	} else {
   276  		return int32(val), nil
   277  	}
   278  }
   279  
   280  func uint64ToInt16(val uint64) (int16, error) {
   281  	if val > math.MaxInt16 {
   282  		return 0, errValueOutOfRange(val)
   283  	} else {
   284  		return int16(val), nil
   285  	}
   286  }
   287  
   288  func uint64ToInt8(val uint64) (int8, error) {
   289  	if val > math.MaxInt8 {
   290  		return 0, errValueOutOfRange(val)
   291  	} else {
   292  		return int8(val), nil
   293  	}
   294  }
   295  
   296  func uintToInt64(val uint) (int64, error) {
   297  	if uint64(val) > math.MaxInt64 {
   298  		return 0, errValueOutOfRange(val)
   299  	} else {
   300  		return int64(val), nil
   301  	}
   302  }
   303  
   304  func uintToInt32(val uint) (int32, error) {
   305  	if val > math.MaxInt32 {
   306  		return 0, errValueOutOfRange(val)
   307  	} else {
   308  		return int32(val), nil
   309  	}
   310  }
   311  
   312  func uintToInt16(val uint) (int16, error) {
   313  	if val > math.MaxInt16 {
   314  		return 0, errValueOutOfRange(val)
   315  	} else {
   316  		return int16(val), nil
   317  	}
   318  }
   319  
   320  func uintToInt8(val uint) (int8, error) {
   321  	if val > math.MaxInt8 {
   322  		return 0, errValueOutOfRange(val)
   323  	} else {
   324  		return int8(val), nil
   325  	}
   326  }
   327  
   328  func uint32ToInt32(val uint32) (int32, error) {
   329  	if val > math.MaxInt32 {
   330  		return 0, errValueOutOfRange(val)
   331  	} else {
   332  		return int32(val), nil
   333  	}
   334  }
   335  
   336  func uint32ToInt16(val uint32) (int16, error) {
   337  	if val > math.MaxInt16 {
   338  		return 0, errValueOutOfRange(val)
   339  	} else {
   340  		return int16(val), nil
   341  	}
   342  }
   343  
   344  func uint32ToInt8(val uint32) (int8, error) {
   345  	if val > math.MaxInt8 {
   346  		return 0, errValueOutOfRange(val)
   347  	} else {
   348  		return int8(val), nil
   349  	}
   350  }
   351  
   352  func uint16ToInt16(val uint16) (int16, error) {
   353  	if val > math.MaxInt16 {
   354  		return 0, errValueOutOfRange(val)
   355  	} else {
   356  		return int16(val), nil
   357  	}
   358  }
   359  
   360  func uint16ToInt8(val uint16) (int8, error) {
   361  	if val > math.MaxInt8 {
   362  		return 0, errValueOutOfRange(val)
   363  	} else {
   364  		return int8(val), nil
   365  	}
   366  }
   367  
   368  func uint8ToInt8(val uint8) (int8, error) {
   369  	if val > math.MaxInt8 {
   370  		return 0, errValueOutOfRange(val)
   371  	} else {
   372  		return int8(val), nil
   373  	}
   374  }
   375  
   376  func stringToInt64(val string) (int64, error) {
   377  	if parsed, err := strconv.ParseInt(val, 10, 64); err != nil {
   378  		return 0, errCannotParseString(val, err)
   379  	} else {
   380  		return parsed, nil
   381  	}
   382  }
   383  
   384  func stringToInt32(val string) (int32, error) {
   385  	if parsed, err := strconv.ParseInt(val, 10, 32); err != nil {
   386  		return 0, errCannotParseString(val, err)
   387  	} else {
   388  		return int32(parsed), nil
   389  	}
   390  }
   391  
   392  func stringToInt16(val string) (int16, error) {
   393  	if parsed, err := strconv.ParseInt(val, 10, 16); err != nil {
   394  		return 0, errCannotParseString(val, err)
   395  	} else {
   396  		return int16(parsed), nil
   397  	}
   398  }
   399  
   400  func stringToInt8(val string) (int8, error) {
   401  	if parsed, err := strconv.ParseInt(val, 10, 8); err != nil {
   402  		return 0, errCannotParseString(val, err)
   403  	} else {
   404  		return int8(parsed), nil
   405  	}
   406  }
   407  
   408  func stringToBigInt(val string) (*big.Int, error) {
   409  	if i, ok := new(big.Int).SetString(val, 10); !ok {
   410  		return nil, errCannotParseString(val, errors.New("big.Int.SetString(text, 10) failed"))
   411  	} else {
   412  		return i, nil
   413  	}
   414  }
   415  
   416  func stringToEpochMillis(val string, layout string, location *time.Location) (int64, error) {
   417  	if parsed, err := time.ParseInLocation(layout, val, location); err != nil {
   418  		return 0, err
   419  	} else {
   420  		return ConvertTimeToEpochMillis(parsed)
   421  	}
   422  }
   423  
   424  func stringToNanosOfDay(val string, layout string) (int64, error) {
   425  	if parsed, err := time.Parse(layout, val); err != nil {
   426  		return 0, err
   427  	} else {
   428  		return ConvertTimeToNanosOfDay(parsed), nil
   429  	}
   430  }
   431  
   432  func stringToEpochDays(val string, layout string) (int32, error) {
   433  	if parsed, err := time.Parse(layout, val); err != nil {
   434  		return 0, err
   435  	} else {
   436  		return ConvertTimeToEpochDays(parsed)
   437  	}
   438  }
   439  
   440  func bigIntToInt64(val *big.Int) (int64, error) {
   441  	if !val.IsInt64() {
   442  		return 0, errValueOutOfRange((*val).String())
   443  	} else {
   444  		return val.Int64(), nil
   445  	}
   446  }
   447  
   448  func bigIntToInt(val *big.Int, intSize int) (int, error) {
   449  	if !val.IsInt64() || (intSize == 32 && (val.Int64() < int64(math.MinInt32) || val.Int64() > int64(math.MaxInt32))) {
   450  		return 0, errValueOutOfRange(val)
   451  	} else {
   452  		return int(val.Int64()), nil
   453  	}
   454  }
   455  
   456  func bigIntToInt32(val *big.Int) (int32, error) {
   457  	if !val.IsInt64() || val.Int64() < math.MinInt32 || val.Int64() > math.MaxInt32 {
   458  		return 0, errValueOutOfRange(val)
   459  	} else {
   460  		return int32(val.Int64()), nil
   461  	}
   462  }
   463  
   464  func bigIntToInt16(val *big.Int) (int16, error) {
   465  	if !val.IsInt64() || val.Int64() < math.MinInt16 || val.Int64() > math.MaxInt16 {
   466  		return 0, errValueOutOfRange(val)
   467  	} else {
   468  		return int16(val.Int64()), nil
   469  	}
   470  }
   471  
   472  func bigIntToInt8(val *big.Int) (int8, error) {
   473  	if !val.IsInt64() || val.Int64() < math.MinInt8 || val.Int64() > math.MaxInt8 {
   474  		return 0, errValueOutOfRange(val)
   475  	} else {
   476  		return int8(val.Int64()), nil
   477  	}
   478  }
   479  
   480  func bigIntToUint64(val *big.Int) (uint64, error) {
   481  	if !val.IsUint64() {
   482  		return 0, errValueOutOfRange(val)
   483  	} else {
   484  		return val.Uint64(), nil
   485  	}
   486  }
   487  
   488  func bigIntToUint(val *big.Int, intSize int) (uint, error) {
   489  	if !val.IsUint64() || (intSize == 32 && val.Uint64() > uint64(math.MaxUint32)) {
   490  		return 0, errValueOutOfRange(val)
   491  	} else {
   492  		return uint(val.Uint64()), nil
   493  	}
   494  }
   495  
   496  func bigIntToUint32(val *big.Int) (uint32, error) {
   497  	if !val.IsUint64() || val.Uint64() > math.MaxUint32 {
   498  		return 0, errValueOutOfRange(val)
   499  	} else {
   500  		return uint32(val.Uint64()), nil
   501  	}
   502  }
   503  
   504  func bigIntToUint16(val *big.Int) (uint16, error) {
   505  	if !val.IsUint64() || val.Uint64() > math.MaxUint16 {
   506  		return 0, errValueOutOfRange(val)
   507  	} else {
   508  		return uint16(val.Uint64()), nil
   509  	}
   510  }
   511  
   512  func bigIntToUint8(val *big.Int) (uint8, error) {
   513  	if !val.IsUint64() || val.Uint64() > math.MaxUint8 {
   514  		return 0, errValueOutOfRange(val)
   515  	} else {
   516  		return uint8(val.Uint64()), nil
   517  	}
   518  }
   519  
   520  func bigFloatToFloat64(val *big.Float) (float64, error) {
   521  	if f64, accuracy := val.Float64(); accuracy != big.Exact {
   522  		return 0, errValueOutOfRange(val)
   523  	} else {
   524  		return f64, nil
   525  	}
   526  }
   527  
   528  func float64ToBigFloat(val float64, dest *big.Float) error {
   529  	if math.IsNaN(val) {
   530  		return errValueOutOfRange(val)
   531  	} else {
   532  		dest.SetFloat64(val)
   533  		return nil
   534  	}
   535  }
   536  
   537  func float64ToFloat32(val float64) (float32, error) {
   538  	// this is the best we can do: convert from float64 to float32 is inherently lossy
   539  	if float64(float32(val)) != val {
   540  		return 0, errValueOutOfRange(val)
   541  	} else {
   542  		return float32(val), nil
   543  	}
   544  }