Skip to content

Commit

Permalink
[CLOUDTRUST-4637] Add conversion functions
Browse files Browse the repository at this point in the history
  • Loading branch information
fperot74 authored Feb 13, 2023
1 parent 43d272e commit ddfd74c
Show file tree
Hide file tree
Showing 3 changed files with 228 additions and 12 deletions.
124 changes: 124 additions & 0 deletions convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package commonservice

import (
"fmt"
"strconv"
"time"
)

const (
layout = "02.01.2006"
)

// ToTimePtr converts an interface{} value as a Time pointer. Error is not possible, defaults to nil
func ToTimePtr(value interface{}) *time.Time {
if value == nil {
return nil
}
switch v := value.(type) {
case time.Time:
return &v
case *time.Time:
return v
case string:
var t, _ = time.Parse(layout, v)
return &t
case *string:
var t, _ = time.Parse(layout, *v)
return &t
}
return nil
}

// ToStringPtr converts an interface{} value as a string pointer
func ToStringPtr(value interface{}) *string {
if value == nil {
return nil
}
switch v := value.(type) {
case *string:
return v
}
var res = fmt.Sprintf("%v", value)
return &res
}

// ToInt converts an interface{} value as int and returns a default value in case of error
func ToInt(value interface{}, defaultValue int) int {
switch v := value.(type) {
case int:
return v
case *int:
return *v
case int64:
return int(v)
case *int64:
return int(*v)
case int32:
return int(v)
case *int32:
return int(*v)
case float32:
return int(v)
case *float32:
return int(*v)
case float64:
return int(v)
case *float64:
return int(*v)
case string:
return atoi(v, defaultValue)
case *string:
return atoi(*v, defaultValue)
default:
return defaultValue
}
}

// ToFloat converts an interface{} value as float64 and returns a default value in case of error
func ToFloat(value interface{}, defaultValue float64) float64 {
switch v := value.(type) {
case int:
return float64(v)
case *int:
return float64(*v)
case int64:
return float64(v)
case *int64:
return float64(*v)
case int32:
return float64(v)
case *int32:
return float64(*v)
case float32:
return float64(v)
case *float32:
return float64(*v)
case float64:
return v
case *float64:
return *v
case string:
return atof(v, defaultValue)
case *string:
return atof(*v, defaultValue)
default:
return defaultValue
}
}

func atoi(value string, defaultValue int) int {
var res, err = strconv.Atoi(value)
if err != nil {
return defaultValue
}
return res
}

func atof(value string, defaultValue float64) float64 {
var res, err = strconv.ParseFloat(value, 64)
if err != nil {
return defaultValue
}
return res
}
90 changes: 90 additions & 0 deletions convert_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package commonservice

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
)

type dummy struct {
}

func ptr(value string) *string {
return &value
}

func toStr(value *time.Time) string {
return value.Format(layout)
}

func TestToTimePtr(t *testing.T) {
var anyTime = time.Now().Add(-2500 * time.Hour) // Why not
var anyTimeAsString = anyTime.Format(layout)
assert.Nil(t, ToTimePtr(nil))
assert.Nil(t, ToTimePtr(dummy{}))
assert.Equal(t, toStr(&anyTime), toStr(ToTimePtr(anyTime)))
assert.Equal(t, toStr(&anyTime), toStr(ToTimePtr(&anyTime)))
assert.Equal(t, toStr(&anyTime), toStr(ToTimePtr(anyTimeAsString)))
assert.Equal(t, toStr(&anyTime), toStr(ToTimePtr(&anyTimeAsString)))
assert.Equal(t, toStr(&anyTime), toStr(ToTimePtr(anyTime)))
}

func TestToStringPtr(t *testing.T) {
var str = "abc"
assert.Nil(t, ToStringPtr(nil))
assert.Equal(t, "2", *ToStringPtr(2))
assert.Equal(t, "2", *ToStringPtr(2.0))
assert.Equal(t, "abc", *ToStringPtr(str))
assert.Equal(t, "abc", *ToStringPtr(&str))
}

func TestToInt(t *testing.T) {
var invalid = -99999
var vInt = 2
var vInt32 = int32(2)
var vInt64 = int64(2)
var vFloat32 = float32(2.0)
var vFloat64 = float64(2.0)
assert.Equal(t, 2, ToInt(ptr("2"), invalid))
assert.Equal(t, 2, ToInt("2", invalid))
assert.Equal(t, invalid, ToInt("2x", invalid))
assert.Equal(t, 2, ToInt(vInt, invalid))
assert.Equal(t, 2, ToInt(&vInt, invalid))
assert.Equal(t, 2, ToInt(vInt32, invalid))
assert.Equal(t, 2, ToInt(&vInt32, invalid))
assert.Equal(t, 2, ToInt(vInt64, invalid))
assert.Equal(t, 2, ToInt(&vInt64, invalid))
assert.Equal(t, 2, ToInt(vFloat32, invalid))
assert.Equal(t, 2, ToInt(&vFloat32, invalid))
assert.Equal(t, 2, ToInt(vFloat64, invalid))
assert.Equal(t, 2, ToInt(&vFloat64, invalid))
assert.Equal(t, invalid, ToInt(time.Now(), invalid))
}

func TestToFloat(t *testing.T) {
var invalid = float64(-99999)
var expected = float64(3.14)
var vInt = 3
var vInt32 = int32(3)
var vInt64 = int64(3)
var vFloat32 = float32(3.14)
var vFloat64 = float64(3.14)
assert.Equal(t, invalid, ToFloat("xxx", invalid))
assert.Equal(t, float64(3), ToFloat(vInt, invalid))
assert.Equal(t, float64(3), ToFloat(&vInt, invalid))
assert.Equal(t, float64(3), ToFloat(vInt32, invalid))
assert.Equal(t, float64(3), ToFloat(&vInt32, invalid))
assert.Equal(t, float64(3), ToFloat(vInt64, invalid))
assert.Equal(t, float64(3), ToFloat(&vInt64, invalid))
var equals = func(a, b float64) bool {
return int64(a*100.0) == int64(b*100)
}
assert.True(t, equals(expected, ToFloat(ptr("3.14"), invalid)))
assert.True(t, equals(expected, ToFloat("3.14", invalid)))
assert.True(t, equals(expected, ToFloat(vFloat32, invalid)))
assert.True(t, equals(expected, ToFloat(&vFloat32, invalid)))
assert.True(t, equals(expected, ToFloat(vFloat64, invalid)))
assert.True(t, equals(expected, ToFloat(&vFloat64, invalid)))
assert.Equal(t, invalid, ToFloat(time.Now(), invalid))
}
26 changes: 14 additions & 12 deletions security/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,20 @@ var (
EVGetEventsSummary = Actions.addAction(BridgeService, EventsAPI, "EV_GetEventsSummary", ScopeRealm)
EVGetUserEvents = Actions.addAction(BridgeService, EventsAPI, "EV_GetUserEvents", ScopeGroup)

KYCGetActions = Actions.addAction(BridgeService, KycAPI, "KYC_GetActions", ScopeGlobal)
KYCGetUserInSocialRealm = Actions.addAction(BridgeService, KycAPI, "KYC_GetUserInSocialRealm", ScopeRealm)
KYCGetUser = Actions.addAction(BridgeService, KycAPI, "KYC_GetUser", ScopeGroup)
KYCGetUserByUsernameInSocialRealm = Actions.addAction(BridgeService, KycAPI, "KYC_GetUserByUsernameInSocialRealm", ScopeRealm)
KYCGetUserByUsername = Actions.addAction(BridgeService, KycAPI, "KYC_GetUserByUsername", ScopeGroup)
KYCValidateUserInSocialRealm = Actions.addAction(BridgeService, KycAPI, "KYC_ValidateUserInSocialRealm", ScopeRealm)
KYCValidateUser = Actions.addAction(BridgeService, KycAPI, "KYC_ValidateUser", ScopeGroup)
KYCSendSmsConsentCodeInSocialRealm = Actions.addAction(BridgeService, KycAPI, "KYC_SendSmsConsentCodeInSocialRealm", ScopeRealm)
KYCSendSmsConsentCode = Actions.addAction(BridgeService, KycAPI, "KYC_SendSmsConsentCode", ScopeGroup)
KYCSendSmsCodeInSocialRealm = Actions.addAction(BridgeService, KycAPI, "KYC_SendSmsCodeInSocialRealm", ScopeRealm)
KYCSendSmsCode = Actions.addAction(BridgeService, KycAPI, "KYC_SendSmsCode", ScopeGroup)
KYCValidateUserBasicID = Actions.addAction(BridgeService, KycAPI, "KYC_ValidateUserBasicID", ScopeRealm) /***TO BE REMOVED WHEN MULTI-ACCREDITATION WILL BE IMPLEMENTED***/ /***TO BE REMOVED WHEN MULTI-ACCREDITATION WILL BE IMPLEMENTED***/
KYCGetActions = Actions.addAction(BridgeService, KycAPI, "KYC_GetActions", ScopeGlobal)
KYCGetRealmUserProfileInSocialRealm = Actions.addAction(BridgeService, KycAPI, "KYC_GetRealmUserProfileInSocialRealm", ScopeRealm)
KYCGetRealmUserProfile = Actions.addAction(BridgeService, KycAPI, "KYC_GetRealmUserProfile", ScopeRealm)
KYCGetUserInSocialRealm = Actions.addAction(BridgeService, KycAPI, "KYC_GetUserInSocialRealm", ScopeRealm)
KYCGetUser = Actions.addAction(BridgeService, KycAPI, "KYC_GetUser", ScopeGroup)
KYCGetUserByUsernameInSocialRealm = Actions.addAction(BridgeService, KycAPI, "KYC_GetUserByUsernameInSocialRealm", ScopeRealm)
KYCGetUserByUsername = Actions.addAction(BridgeService, KycAPI, "KYC_GetUserByUsername", ScopeGroup)
KYCValidateUserInSocialRealm = Actions.addAction(BridgeService, KycAPI, "KYC_ValidateUserInSocialRealm", ScopeRealm)
KYCValidateUser = Actions.addAction(BridgeService, KycAPI, "KYC_ValidateUser", ScopeGroup)
KYCSendSmsConsentCodeInSocialRealm = Actions.addAction(BridgeService, KycAPI, "KYC_SendSmsConsentCodeInSocialRealm", ScopeRealm)
KYCSendSmsConsentCode = Actions.addAction(BridgeService, KycAPI, "KYC_SendSmsConsentCode", ScopeGroup)
KYCSendSmsCodeInSocialRealm = Actions.addAction(BridgeService, KycAPI, "KYC_SendSmsCodeInSocialRealm", ScopeRealm)
KYCSendSmsCode = Actions.addAction(BridgeService, KycAPI, "KYC_SendSmsCode", ScopeGroup)
KYCValidateUserBasicID = Actions.addAction(BridgeService, KycAPI, "KYC_ValidateUserBasicID", ScopeRealm) /***TO BE REMOVED WHEN MULTI-ACCREDITATION WILL BE IMPLEMENTED***/ /***TO BE REMOVED WHEN MULTI-ACCREDITATION WILL BE IMPLEMENTED***/

MGMTGetActions = Actions.addAction(BridgeService, ManagementAPI, "MGMT_GetActions", ScopeGlobal)
MGMTGetRealms = Actions.addAction(BridgeService, ManagementAPI, "MGMT_GetRealms", ScopeGlobal)
Expand Down

0 comments on commit ddfd74c

Please sign in to comment.