utils

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2025 License: Apache-2.0 Imports: 25 Imported by: 0

README

utils

Table of Contents

  1. Description
  2. Structure and Organisation
  3. Class Diagram
  4. Functionality
  5. Data Types
  6. Testing
  7. Proposed Functionality/Requirements
  8. References

Specification

Description

This package contains utility tools and functionalities used by other packages

Structure and Organisation

Here is quick overview of the contents of this directory:

  • README: Current file which is aimed towards developers who wish to use and modify the package functionality.

  • blockchain: This file contains methods and data types related to interaction with blockchain.

  • file_system: This file contains a method to retrieve the size of the volume.

  • init: This file initializes an Open Telemetry logger for this package. It also defines constants to reflect the status of transaction.

  • network: This file contains helper methods for DMS API calls and responses.

  • progress_io: This file defines wrapper functions for readers and writers with progress tracking capabilities.

  • syncmap: This file defines a SyncMap type which is a thread-safe version of the standard Go map with strongly-typed methods and functions for managing key-value pairs concurrently.

  • utils: This file contains various utility functions for the DMS functionality.

  • validate: This contains helper functions that perform different kinds of validation checks and numeric conversions.

  • specs: This folder contains the class diagram for the package.

Files with *_test.go naming contains unit tests of the specified functionality.

Class Diagram
Source File

utils Class Diagram

Rendered from source file
!$rootUrlGitlab = "https://gitlab.com/nunet/device-management-service/-/raw/main"
!$packageRelativePath = "/utils"
!$packageUrlGitlab = $rootUrlGitlab + $packageRelativePath
 
!include $packageUrlGitlab/specs/class_diagram.puml
Functionality

utils package defines various helper methods for functionality defined in the different packages of DMS. Refer to utils.go for details.

Data Types

Blockchain data models

  • utils.UTXOs: TBD
type UTXOs struct {
	TxHash  string `json:"tx_hash"`
	IsSpent bool   `json:"is_spent"`
}
  • utils.TxHashResp: TBD
type TxHashResp struct {
	TxHash          string `json:"tx_hash"`
	TransactionType string `json:"transaction_type"`
	DateTime        string `json:"date_time"`
}
  • utils.ClaimCardanoTokenBody: TBD
type ClaimCardanoTokenBody struct {
	ComputeProviderAddress string `json:"compute_provider_address"`
	TxHash                 string `json:"tx_hash"`
}
  • utils.rewardRespToCPD: TBD
type rewardRespToCPD struct {
	ServiceProviderAddr string `json:"service_provider_addr"`
	ComputeProviderAddr string `json:"compute_provider_addr"`
	RewardType          string `json:"reward_type,omitempty"`
	SignatureDatum      string `json:"signature_datum,omitempty"`
	MessageHashDatum    string `json:"message_hash_datum,omitempty"`
	Datum               string `json:"datum,omitempty"`
	SignatureAction     string `json:"signature_action,omitempty"`
	MessageHashAction   string `json:"message_hash_action,omitempty"`
	Action              string `json:"action,omitempty"`
}
  • utils.UpdateTxStatusBody: TBD
type UpdateTxStatusBody struct {
	Address string `json:"address,omitempty"`
}

progress_io data models

  • utils.IOProgress: TBD
type IOProgress struct {
	n         float64
	size      float64
	started   time.Time
	estimated time.Time
	err       error
}
  • utils.Reader: TBD
type Reader struct {
	reader   io.Reader
	lock     sync.RWMutex
	Progress IOProgress
}
  • utils.Writer: TBD
type Writer struct {
	writer   io.Writer
	lock     sync.RWMutex
	Progress IOProgress
}

syncmap data models

  • utils.SyncMap: a concurrency-safe sync.Map that uses strongly-typed method signatures to ensure the types of its stored data are known.
type SyncMap[K comparable, V any] struct {
	sync.Map
}
Testing

The unit tests for the functionality are defined in network_test.go and utils_test.go files.

Proposed Functionality / Requirements

List of issues related to the implementation of the utils package can be found below. These include proposals for modifications to the package or new functionality needed to cover the requirements of other packages.

References

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrOperationCancelled = errors.New("operation cancelled by user")

Functions

func ConvertTypedSliceToUntypedSlice added in v0.5.0

func ConvertTypedSliceToUntypedSlice(typedSlice interface{}) []interface{}

func CreateDirIfNotExists added in v0.6.1

func CreateDirIfNotExists(fs afero.Afero, path string) error

func CreateTarArchive added in v0.6.0

func CreateTarArchive(afs afero.Afero, sourceDir, targetFile string) error

CreateTarArchive creates a tar archive of the source directory

func CurrentFileDirectory added in v0.6.1

func CurrentFileDirectory() string

CurrentFileDirectory returns the path of this file

func FileExists added in v0.5.0

func FileExists(fs afero.Fs, filename string) bool

FileExists checks if destination file exists

func GetDirectorySize added in v0.5.0

func GetDirectorySize(fs afero.Fs, path string) (int64, error)

func IsSameShallowType added in v0.5.0

func IsSameShallowType(a, b interface{}) bool

func IsStrictlyContained added in v0.5.0

func IsStrictlyContained(leftSlice, rightSlice []interface{}) bool

IsStrictlyContained checks if all elements of rightSlice are contained in leftSlice

func IsStrictlyContainedInt added in v0.5.0

func IsStrictlyContainedInt(leftSlice, rightSlice []int) bool

IsStrictlyContainedInt checks if all elements of rightSlice are contained in leftSlice

func MapKeysToSlice added in v0.6.1

func MapKeysToSlice[R comparable, T any](m map[R]T) []R

func PromptForPassphrase added in v0.6.0

func PromptForPassphrase(confirm bool) (string, error)

func PromptReonboard added in v0.6.0

func PromptReonboard(r io.Reader, w io.Writer) error

PromptReonboard is a wrapper of utils.PromptYesNo with custom prompt that return error if user declines reonboard

func PromptYesNo added in v0.4.159

func PromptYesNo(in io.Reader, out io.Writer, prompt string) (bool, error)

PromptYesNo loops on confirmation from user until valid answer

func RandomString

func RandomString(n int) (string, error)

RandomString generates a random string of length n

func SanitizeArchivePath added in v0.5.0

func SanitizeArchivePath(d, t string) (v string, err error)

Sanitize archive file pathing from "G305: Zip Slip vulnerability"

func SliceContains added in v0.4.159

func SliceContains(s []string, str string) bool

SliceContains checks if a string exists in a slice

func WriteToFile added in v0.5.0

func WriteToFile(fs afero.Fs, data []byte, filePath string) (string, error)

WriteToFile writes data to a file.

Types

type HTTPClient added in v0.5.0

type HTTPClient struct {
	BaseURL    string
	APIVersion string
	Client     *http.Client
}

func NewHTTPClient added in v0.5.0

func NewHTTPClient(baseURL, version string) *HTTPClient

NewHTTPClient creates a new HTTPClient with APM instrumentation.

func (*HTTPClient) MakeRequest added in v0.5.0

func (c *HTTPClient) MakeRequest(ctx context.Context, method, relativePath string, body []byte) ([]byte, int, error)

MakeRequest performs an HTTP request with the given method, path, and body. It returns the response body, status code, and an error if any.

type SyncMap added in v0.5.0

type SyncMap[K comparable, V any] struct {
	sync.Map
}

A SyncMap is a concurrency-safe sync.Map that uses strongly-typed method signatures to ensure the types of its stored data are known.

func SyncMapFromMap added in v0.5.0

func SyncMapFromMap[K comparable, V any](m map[K]V) *SyncMap[K, V]

SyncMapFromMap converts a standard Go map to a concurrency-safe SyncMap.

func (*SyncMap[K, V]) Get added in v0.5.0

func (m *SyncMap[K, V]) Get(key K) (V, bool)

Get retrieves the value associated with the given key from the map. It returns the value and a boolean indicating whether the key was found.

func (*SyncMap[K, V]) Iter added in v0.5.0

func (m *SyncMap[K, V]) Iter(ranger func(key K, value V) bool)

Iter iterates over each key-value pair in the map, executing the provided function on each pair. The iteration stops if the provided function returns false.

func (*SyncMap[K, V]) Keys added in v0.5.0

func (m *SyncMap[K, V]) Keys() []K

Keys returns a slice containing all the keys present in the map.

func (*SyncMap[K, V]) Put added in v0.5.0

func (m *SyncMap[K, V]) Put(key K, value V)

Put inserts or updates a key-value pair in the map.

func (*SyncMap[K, V]) String added in v0.5.0

func (m *SyncMap[K, V]) String() string

String provides a string representation of the map, listing all key-value pairs.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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