go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/projects/nodes/pkg/incrutil/constants.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package incrutil
     9  
    10  import "go.charczuk.com/sdk/iter"
    11  
    12  const (
    13  	NodeTypeVar                   = "var"
    14  	NodeTypeTable                 = "table"
    15  	NodeTypeObserver              = "observer"
    16  	NodeTypeCutoffExpression      = "cutoff_expression"
    17  	NodeTypeCutoffEpsilon         = "cutoff_epsilon"
    18  	NodeTypeCutoffUnchanged       = "cutoff_unchanged"
    19  	NodeTypeExpression            = "expression"
    20  	NodeTypeFilterTableExpression = "filter_table_expression"
    21  	NodeTypeAlways                = "always"
    22  
    23  	// NodeTypeStatFile = "stat_file"
    24  	// NodeTypeReadFile = "read_file"
    25  	// NodeTypeReadCSV  = "read_csv"
    26  
    27  	NodeTypeFetchCSV  = "fetch_csv"
    28  	NodeTypeFetchJSON = "fetch_json"
    29  
    30  	NodeTypeTimeseriesChart = "timeseries_chart"
    31  	NodeTypeScatterChart    = "scatter_chart"
    32  
    33  	NodeTypeCurrentTimestamp = "current_timestamp"
    34  
    35  	NodeTypeCast             = "cast"
    36  	NodeTypeFirst            = "first"
    37  	NodeTypeIndex            = "index"
    38  	NodeTypeLast             = "last"
    39  	NodeTypeLength           = "length"
    40  	NodeTypeMax              = "max"
    41  	NodeTypeMean             = "mean"
    42  	NodeTypeMedian           = "median"
    43  	NodeTypeMedianSorted     = "median_sorted"
    44  	NodeTypeMerge            = "merge"
    45  	NodeTypeMin              = "min"
    46  	NodeTypePercentile       = "percentile"
    47  	NodeTypePercentileSorted = "percentile_sorted"
    48  	NodeTypeReverse          = "reverse"
    49  	NodeTypeSort             = "sort"
    50  	NodeTypeSum              = "sum"
    51  
    52  	NodeTypeTableColumn = "table_column"
    53  )
    54  
    55  // ValueTypeTable is a special type that can hold
    56  // rows and columns of data.
    57  var ValueTypeTable = "*types.Table"
    58  
    59  // ValueTypeSVG is a special type that can hold an svg.
    60  var ValueTypeSVG = "types.SVG"
    61  
    62  // ValueTypeAny is a special type that can be any value.
    63  //
    64  // It is only used, really, for expressions.
    65  var ValueTypeAny = "any"
    66  
    67  // ValueTypeAnyArray is a special type that can be any value.
    68  //
    69  // It is only used, really, for expressions.
    70  var ValueTypeAnyArray = "[]any"
    71  
    72  const (
    73  	ValueTypeBool      = "bool"
    74  	ValueTypeInt64     = "int64"
    75  	ValueTypeFloat64   = "float64"
    76  	ValueTypeString    = "string"
    77  	ValueTypeTimestamp = "time.Time"
    78  	ValueTypeDuration  = "time.Duration"
    79  )
    80  
    81  // ValueScalarTypes are the integral non-array types.
    82  //
    83  // This list is used, for example, by `nodegen` to generate
    84  // generic type parameters for functions. They need to be go types
    85  // for this reason, but we can translate them to the abstracted api types separately.
    86  var ValueScalarTypes = []string{
    87  	ValueTypeBool,
    88  	ValueTypeInt64,
    89  	ValueTypeFloat64,
    90  	ValueTypeString,
    91  	ValueTypeTimestamp,
    92  	ValueTypeDuration,
    93  }
    94  
    95  // APIValueTypeForGoType is the api type for a given go value type.
    96  func APIValueTypeForGoType(t string) string {
    97  	switch t {
    98  	case "time.Time":
    99  		return "timestamp"
   100  	case "[]time.Time":
   101  		return "[]timestamp"
   102  	case "time.Duration":
   103  		return "duration"
   104  	case "[]time.Duration":
   105  		return "[]duration"
   106  	case "types.Currency":
   107  		return "currency"
   108  	case "[]types.Currency":
   109  		return "[]currency"
   110  	case "*types.Table":
   111  		return "table"
   112  	case "types.SVG":
   113  		return "svg"
   114  	default: // typical case is they map directly
   115  		return t
   116  	}
   117  }
   118  
   119  // ValueArrayTypes are the scalar types as arrays, basically.
   120  var ValueArrayTypes = iter.Apply(ValueScalarTypes, func(v string) string {
   121  	return "[]" + v
   122  })
   123  
   124  // ValueTypes are all the legal values types we can use as inputs
   125  // or outputs for nodes.
   126  var ValueTypes = append(ValueScalarTypes, ValueArrayTypes...)
   127  
   128  // ValueTypesFull are all the legal value types we can use
   129  // as inputs and outputs for nodes including the table and svg types.
   130  var ValueTypesFull = append(ValueTypes, ValueTypeTable, ValueTypeSVG, ValueTypeAny, ValueTypeAnyArray)
   131  
   132  // ValueTypeIsMath returns if a given value type is operatable.
   133  func ValueTypeIsMath(typeName string) bool {
   134  	switch typeName {
   135  	case "int64", "float64", "types.Currency", "time.Duration":
   136  		return true
   137  	case "[]int64", "[]float64", "[]types.Currency", "[]time.Duration":
   138  		return true
   139  	default:
   140  		return false
   141  	}
   142  }