utils

package
v0.0.0-...-aeb4a1d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 5, 2022 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package utils contains convenience, helper, and utility functions.

Index

Examples

Constants

View Source
const MaxFloat64ToPaddedString float64 = 1000000000000

MaxFloat64ToPaddedString is the max absolute value of float64's that can be converted to string.

Variables

View Source
var RE_stripFnPreamble = regexp.MustCompile(`^.*\.(.*)$`)

RE_stripFnPreamble uses regex to extract function names (and not the module path).

Functions

func AddToSet

func AddToSet(items []string, item string) []string

AddToSet adds an item to a set.

Example
set := []string{"item1", "item2", "item3"}

setNew := AddToSet(set, "item4")

fmt.Println(setNew)
Output:

[item1 item2 item3 item4]

func CheckParam

func CheckParam(args []string, param string) bool

CheckParam checks if param is in args.

Example
args := []string{"arg1", "arg2", "arg3"}

param1 := "arg1"
includesParam1 := CheckParam(args, param1)
fmt.Println(includesParam1)

param2 := "X"
includesParam2 := CheckParam(args, param2)
fmt.Println(includesParam2)
Output:

true
false

func ConvertToString

func ConvertToString(val interface{}) (string, error)

ConvertToString determines the type of val and returns it as a string. Valid types for val are bool, int, int64, float64, or string. If val is an int, int64, or float64, its absolute value cannot exceed utils.MaxFloat64ToPaddedString. This function is not to be used for general conversion to string, as the strings produced by this function may not look at all like the input. Use this function to produce strings that are suitable for querying assets by index.

func DateStringToTimestamp

func DateStringToTimestamp(date string) (int64, error)

DateStringToTimestamp converts a date string to a timestamp.

Example
dateString := "2009-02-13 18:31:30 -0500 EST"

timestamp, _ := DateStringToTimestamp(dateString)

fmt.Println(timestamp)
Output:

1234567890

func EnterFnLog

func EnterFnLog() string

EnterFnLog logs and returns the current function name at the start of function execution. (DEPRECATED) use EnterFnLogger function instead

Example
functionName := EnterFnLog()

fmt.Println(functionName)
Output:

ExampleEnterFnLog

func EnterFnLogger

func EnterFnLogger(mylogger *shim.ChaincodeLogger) string

EnterFnLogger logs and returns the current function name at the start of function execution.

func EqualStringArrays

func EqualStringArrays(val1 []string, val2 []string) bool

EqualStringArrays returns if two string arrays are the same.

func ExitFnLog

func ExitFnLog(s string)

ExitFnLog logs the current function name at the end of function execution. (DEPRECATED) use ExitFnLogger function instead

Example
defer ExitFnLog(EnterFnLog())
Output:

func ExitFnLogger

func ExitFnLogger(mylogger *shim.ChaincodeLogger, s string)

ExitFnLogger logs the current function name at the end of execution.

func FilterDataList

func FilterDataList(dataList []string, filter []string) []string

FilterDataList filters dataList by filter.

Example
list := []string{"key1:val1", "key2:val2", "key3:val3", "key4:val4"}
filterList := []string{"val1", "val2", "val3"}

listNew := FilterDataList(list, filterList)

fmt.Println(listNew)
Output:

[key1:val1 key2:val2 key3:val3]

func FilterOutFromSet

func FilterOutFromSet(itemList []string, filterList []string) []string

FilterOutFromSet removes elements from itemList that are in filterList and returns the result as a set.

Example
list := []string{"item1", "item2", "item3", "item4"}
filterList := []string{"item1", "item2", "item3"}

set := FilterOutFromSet(list, filterList)

fmt.Println(set)
Output:

[item4]

func FilterSet

func FilterSet(itemList []string, filterList []string) []string

FilterSet removes elements from itemList that are not in filterList and returns the result as a set.

Example
list := []string{"item1", "item2", "item3", "item4"}
filterList := []string{"item1", "item2", "item3"}

set := FilterSet(list, filterList)

fmt.Println(set)
Output:

[item1 item2 item3]

func Float64ToPaddedString

func Float64ToPaddedString(num float64) (string, error)

Float64ToPaddedString converts a float64 to an 18-character string padded with "0"s at the front. 4 decimal places are used. The number must be less than 12 digits (excluding decimals). MaxFloat64ToPaddedString is added to the number so that all negative numbers become positive. Therefore, negative numbers will start with a "0" and positive numbers will start with a "1". The actual number will follow.

Example
paddedStringPos, _ := Float64ToPaddedString(5.1234)
fmt.Println(paddedStringPos)

paddedStringNeg, _ := Float64ToPaddedString(-5.1234)
fmt.Println(paddedStringNeg)
Output:

1000000000005.1234
0999999999994.8766

func GenerateRandomBytes

func GenerateRandomBytes(n int) ([]byte, error)

GenerateRandomBytes returns securely generated random bytes. It will return an error if the system's secure random number generator fails to function correctly, in which case the caller should not continue.

func GetDataList

func GetDataList(data map[string]bool) []string

GetDataList returns a sorted list of strings from data map.

Example
m := make(map[string]bool)
m["a"] = true
m["b"] = true
m["x"] = true
m["l"] = true

list := GetDataList(m)

fmt.Println(list)
Output:

[a b l x]

func GetDataMap

func GetDataMap(data []string, access []string) map[string]bool

GetDataMap expands data and returns a data map.

Example
data1 := []string{"item1", "item2", "item3"}
GetDataMap(data1, nil)
// map[item1:true item2:true item3:true]

data2 := []string{"item1:read", "item1:read", "item2:read"}
GetDataMap(data2, nil)
// map[item1:read:true item1:write:true item2:read:true]

access := []string{"read", "write"}
GetDataMap(data1, access)
// map[item1:read:true item1:write:true item2:read:true item2:write:true item3:read:true item3:write:true ]
Output:

func GetSet

func GetSet(arg string) []string

GetSet parses a comma-separated string and returns a sorted set of strings.

Example
listStr := "item1, item2, item3, item3"

set := GetSet(listStr)

fmt.Println(set)
Output:

[item1 item2 item3]

func GetSetFromList

func GetSetFromList(items []string) []string

GetSetFromList converts a list of strings into a sorted set of strings.

Example
list := []string{"item1", "item3", "item2", "item1"}

set := GetSetFromList(list)

fmt.Println(set)
Output:

[item1 item2 item3]

func InList

func InList(listdata []string, item string) bool

InList returns true if item is in listdata, false otherwise.

Example
list := []string{"item1", "item2", "item3"}

item1 := "item1"
inList1 := InList(list, item1)
fmt.Println(inList1)

item2 := "X"
inList2 := InList(list, item2)
fmt.Println(inList2)
Output:

true
false

func InsertInt

func InsertInt(slice []int, value int) []int

InsertInt inserts value into a sorted list of ints.

Example
list := []int{1, 2, 4}

listNew := InsertInt(list, 3)

fmt.Println(listNew)
Output:

[1 2 3 4]

func InsertString

func InsertString(slice []string, value string) []string

InsertString inserts a value to sorted list of strings.

Example
list := []string{"a", "c", "d"}

listNew := InsertString(list, "b")

fmt.Println(listNew)
Output:

[a b c d]

func IsInstanceOf

func IsInstanceOf(object, objectType interface{}) bool

IsInstanceOf returns true if object and objectType are of the same type, false otherwise.

Example
customErr := &custom_errors.MarshalError{Type: "object"}

isInstanceOf1 := IsInstanceOf(customErr, &custom_errors.MarshalError{})
fmt.Println(isInstanceOf1)

isInstanceOf2 := IsInstanceOf(customErr, &custom_errors.UnmarshalError{})
fmt.Println(isInstanceOf2)
Output:

true
false

func IsStringEmpty

func IsStringEmpty(s interface{}) bool

IsStringEmpty returns true if the provided string is nil or empty, false otherwise.

Example
string1 := "string1"
isEmpty1 := IsStringEmpty(string1)
fmt.Println(isEmpty1)

string2 := ""
isEmpty2 := IsStringEmpty(string2)
fmt.Println(isEmpty2)

var string3 string
isEmpty3 := IsStringEmpty(string3)
fmt.Println(isEmpty3)

bool1 := false
isEmpty4 := IsStringEmpty(bool1)
fmt.Println(isEmpty4)
Output:

false
true
true
true

func Map

func Map(args []string, f func(string) string) []string

Map applies function f to each element of args.

Example
list := []string{"item1", "item2", "item3"}

listNew := Map(list, strings.Title)

fmt.Println(listNew)
Output:

[Item1 Item2 Item3]

func NormalizeDataList

func NormalizeDataList(data []string) []string

NormalizeDataList returns a normalized data list.

Example
list := []string{"item1", "item2", "item3", "item3"}

normalizedList := NormalizeDataList(list)

fmt.Println(normalizedList)
Output:

[item1 item2 item3]

func PerformHTTP

func PerformHTTP(method string, url string, requestBody []byte, headers map[string]string, user []byte, pass []byte, retry ...int) (*http.Response, []byte, error)

PerformHTTP performs an HTTP request and returns the response.

Example
headers := make(map[string]string)
headers["Content-Type"] = "application/json"
data := `{"id": "myID", "data": "my data"}`
PerformHTTP("GET", "https://www.example.com/", []byte(data), headers, []byte("user"), []byte("password"))
Output:

func RemoveFromSet

func RemoveFromSet(itemList []string, item string) []string

RemoveFromSet removes an item from a set.

Example
set := []string{"item1", "item2", "item3", "item4"}

setNew := RemoveFromSet(set, "item4")

fmt.Println(setNew)
Output:

[item1 item2 item3]

func RemoveInt

func RemoveInt(slice []int, value int) []int

RemoveInt removes value from a sorted list of ints.

Example
list := []int{1, 2, 3, 4}

listNew := RemoveInt(list, 4)

fmt.Println(listNew)
Output:

[1 2 3]

func RemoveItemFromList

func RemoveItemFromList(list []string, item string) []string

RemoveItemFromList returns list with the first element that matches item removed.

Example
list := []string{"item1", "item2", "item3"}

newList1 := RemoveItemFromList(list, "item1")

fmt.Println(newList1)
Output:

[item2 item3]

func RemoveString

func RemoveString(slice []string, value string) []string

RemoveString removes value from a sorted list of ints.

Example
list := []string{"a", "b", "c", "d"}

listNew := RemoveString(list, "d")

fmt.Println(listNew)
Output:

[a b c]

func SetLogLevel

func SetLogLevel(logLevel shim.LoggingLevel)

SetLogLevel sets the log level.

func TimestampToDateString

func TimestampToDateString(ts int64) string

TimestampToDateString converts a timestamp to an EST date string.

Example
timestamp := int64(1234567890)

dateString := TimestampToDateString(timestamp)

fmt.Println(dateString)
Output:

2009-02-13 18:31:30 -0500 EST

func ToQueryString

func ToQueryString(m map[string][]string) string

ToQueryString returns a map of data into query parameters.

Example
queryMap := make(map[string][]string)
queryMap["provider"] = []string{"provider1"}
queryMap["patient"] = []string{"patient1"}

queryString := ToQueryString(queryMap)

fmt.Println(queryString)
Output:

patient=patient1&provider=provider1

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL