mirror of
https://github.com/hyperledger/fabric-samples.git
synced 2026-06-17 23:45:10 +00:00
Created parser, contract and utils packages and extracted each piece of functionality into its own files. Removed "Get" prefix from methods and changed return values from interfaces to structs. Signed-off-by: Stanislav Jakuschevskij <stas@two-giants.com>
39 lines
709 B
Go
39 lines
709 B
Go
package utils
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"errors"
|
|
"math/big"
|
|
)
|
|
|
|
func RandomElement(values []string) string {
|
|
result := values[RandomInt(len(values))]
|
|
return result
|
|
}
|
|
|
|
func RandomInt(max int) int {
|
|
result, err := rand.Int(rand.Reader, big.NewInt(int64(max)))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return int(result.Int64())
|
|
}
|
|
|
|
func DifferentElement(values []string, currentValue string) string {
|
|
candidateValues := []string{}
|
|
for _, v := range values {
|
|
if v != currentValue {
|
|
candidateValues = append(candidateValues, v)
|
|
}
|
|
}
|
|
return RandomElement(candidateValues)
|
|
}
|
|
|
|
func AssertDefined[T any](value T, message string) T {
|
|
if any(value) == any(nil) {
|
|
panic(errors.New(message))
|
|
}
|
|
|
|
return value
|
|
}
|