Skip to main content

format.go

Overview - format.go

This file provides utility functions for formatting data to be used in ClickHouse queries and for validating and casting values based on their data type.

Detailed Documentation

ValidateAndCastValue

  • Purpose: Validates and casts a value to the corresponding data type.
  • Parameters:
    • v (interface{}): The value to validate and cast.
    • dataType (v3.AttributeKeyDataType): The expected data type.
  • Returns:
    • interface{}: The validated and cast value.
    • error: An error if validation or casting fails.

QuoteEscapedString

  • Purpose: Escapes a string for use in ClickHouse queries.
  • Parameters:
    • str (string): The string to escape.
  • Returns:
    • string: The escaped string.

QuoteEscapedStringForContains

  • Purpose: Escapes a string for use in ClickHouse LIKE queries, which are used for contains.
  • Parameters:
    • str (string): The string to escape.
    • isIndex (bool): indicates whether the string comes from an indexed source and contains an extra slash
  • Returns:
    • string: The escaped string.

ClickHouseFormattedValue

  • Purpose: Formats a value to be used in a ClickHouse query.
  • Parameters:
    • v (interface{}): The value to format.
  • Returns:
    • string: The formatted value.

ClickHouseFormattedMetricNames

  • Purpose: Formats Metric names to be used in ClickHouse query
  • Parameters:
    • v (interface{}): The value to format.
  • Returns:
    • string: The formatted value.

AddBackTickToFormatTag

  • Purpose: Adds backticks to a tag if it contains a dot (.).
  • Parameters:
    • str (string): The tag to format.
  • Returns:
    • string: The formatted tag.

AddBackTickToFormatTags

  • Purpose: AddBackTickToFormatTag wrapper
  • Parameters:
    • inputs (...string): A slice of tags.
  • Returns:
    • []string: The formatted slice of tags.

GetClickhouseColumnName

  • Purpose: Generates column name for clickhouse.
  • Parameters:
    • typeName (string): The type of name.
    • dataType (string): The data type.
    • field (string): The field name.
  • Returns:
    • colName (string): The generated column name.

GetClickhouseColumnNameV2

  • Purpose: Generates column name for clickhouse.
  • Parameters:
    • typeName (string): The type of name.
    • dataType (string): The data type.
    • field (string): The field name.
  • Returns:
    • colName (string): The generated column name.

GetEpochNanoSecs

  • Purpose: GetEpochNanoSecs takes epoch and returns it in ns.
  • Parameters:
    • epoch (int64): The epoch value.
  • Returns:
    • int64: Epoch in nanoseconds.

NormalizeMap

  • Purpose: Normalizes the map from uint64 to float64
  • Parameters:
    • data (map[string]uint64): The map to be normalized.
  • Returns:
    • map[string]float64: Normalized map.

Code Examples

// Example of validating and casting a value
const value, err = ValidateAndCastValue("123", v3.AttributeKeyDataTypeInt64);

// Example of formatting a value for ClickHouse
const formattedValue = ClickHouseFormattedValue("test string");

Include in Getting Started: NO