dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts@v1.0.2/common/utils.go (about)

     1  //
     2  // Copyright (C) 2020-2023 IOTech Ltd
     3  //
     4  // SPDX-License-Identifier: Apache-2.0
     5  
     6  package common
     7  
     8  import (
     9  	"fmt"
    10  	"net/url"
    11  	"strings"
    12  
    13  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/errors"
    14  )
    15  
    16  var valueTypes = []string{
    17  	ValueTypeBool, ValueTypeString,
    18  	ValueTypeUint8, ValueTypeUint16, ValueTypeUint32, ValueTypeUint64,
    19  	ValueTypeInt8, ValueTypeInt16, ValueTypeInt32, ValueTypeInt64,
    20  	ValueTypeFloat32, ValueTypeFloat64,
    21  	ValueTypeBinary,
    22  	ValueTypeBoolArray, ValueTypeStringArray,
    23  	ValueTypeUint8Array, ValueTypeUint16Array, ValueTypeUint32Array, ValueTypeUint64Array,
    24  	ValueTypeInt8Array, ValueTypeInt16Array, ValueTypeInt32Array, ValueTypeInt64Array,
    25  	ValueTypeFloat32Array, ValueTypeFloat64Array,
    26  	ValueTypeObject,
    27  }
    28  
    29  // NormalizeValueType normalizes the valueType to upper camel case
    30  func NormalizeValueType(valueType string) (string, error) {
    31  	for _, v := range valueTypes {
    32  		if strings.EqualFold(valueType, v) {
    33  			return v, nil
    34  		}
    35  	}
    36  	return "", errors.NewCommonEdgeX(errors.KindContractInvalid, fmt.Sprintf("unable to normalize the unknown value type %s", valueType), nil)
    37  }
    38  
    39  // BuildTopic is a helper function to build MessageBus topic from multiple parts
    40  func BuildTopic(parts ...string) string {
    41  	return strings.Join(parts, "/")
    42  }
    43  
    44  // URLEncode encodes the input string with additional common character support
    45  func URLEncode(s string) string {
    46  	res := url.PathEscape(s)
    47  	res = strings.Replace(res, "+", "%2B", -1) // MQTT topic reserved char
    48  	res = strings.Replace(res, "-", "%2D", -1)
    49  	res = strings.Replace(res, ".", "%2E", -1) // RegexCmd and Redis topic reserved char
    50  	res = strings.Replace(res, "_", "%5F", -1)
    51  	res = strings.Replace(res, "~", "%7E", -1)
    52  
    53  	return res
    54  }
    55  
    56  type pathBuilder struct {
    57  	sb                    strings.Builder
    58  	enableNameFieldEscape bool
    59  }
    60  
    61  func NewPathBuilder() *pathBuilder {
    62  	return &pathBuilder{}
    63  }
    64  
    65  func (b *pathBuilder) EnableNameFieldEscape(enableNameFieldEscape bool) *pathBuilder {
    66  	b.enableNameFieldEscape = enableNameFieldEscape
    67  	return b
    68  }
    69  
    70  func (b *pathBuilder) SetPath(path string) *pathBuilder {
    71  	b.sb.WriteString(path + "/")
    72  	return b
    73  }
    74  
    75  // SetNameFieldPath set name path, such as device name, profile name, interval name
    76  func (b *pathBuilder) SetNameFieldPath(namePath string) *pathBuilder {
    77  	if b.enableNameFieldEscape {
    78  		namePath = URLEncode(namePath)
    79  	}
    80  	b.sb.WriteString(namePath + "/")
    81  	return b
    82  }
    83  
    84  func (b *pathBuilder) BuildPath() string {
    85  	return strings.TrimSuffix(b.sb.String(), "/")
    86  }