go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/projects/nodes/pkg/model/inode_from_node.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 model
     9  
    10  import (
    11  	"fmt"
    12  
    13  	"github.com/wcharczuk/go-incr"
    14  
    15  	"go.charczuk.com/projects/nodes/pkg/incrutil"
    16  	"go.charczuk.com/projects/nodes/pkg/model/gen"
    17  	"go.charczuk.com/projects/nodes/pkg/types"
    18  )
    19  
    20  // INodeFromNode returns an incremental node from a given api node.
    21  func INodeFromNode(graph *incr.Graph, apiNode *types.Node) (incr.INode, error) {
    22  	var output incr.INode
    23  	var err error
    24  	switch apiNode.Metadata.NodeType {
    25  	case incrutil.NodeTypeVar:
    26  		output, err = gen.VarForNode(graph, apiNode)
    27  	case incrutil.NodeTypeTable:
    28  		output, err = gen.TableForNode(graph, apiNode)
    29  	case incrutil.NodeTypeCast:
    30  		output, err = gen.CastForNode(graph, apiNode)
    31  	case incrutil.NodeTypeMerge:
    32  		output, err = gen.MergeForNode(graph, apiNode)
    33  	case incrutil.NodeTypeCutoffUnchanged:
    34  		output, err = gen.CutoffUnchangedForNode(graph, apiNode)
    35  	case incrutil.NodeTypeCutoffExpression:
    36  		output, err = gen.CutoffExpressionForNode(graph, apiNode)
    37  	case incrutil.NodeTypeCutoffEpsilon:
    38  		output, err = gen.CutoffEpsilonForNode(graph, apiNode)
    39  	case incrutil.NodeTypeExpression:
    40  		output, err = gen.ExpressionForNode(graph, apiNode)
    41  	case incrutil.NodeTypeFilterTableExpression:
    42  		output, err = gen.FilterTableExpressionForNode(graph, apiNode)
    43  	case incrutil.NodeTypeSum:
    44  		output, err = gen.SumForNode(graph, apiNode)
    45  	case incrutil.NodeTypeMean:
    46  		output, err = gen.MeanForNode(graph, apiNode)
    47  	case incrutil.NodeTypeMedian:
    48  		output, err = gen.MedianForNode(graph, apiNode)
    49  	case incrutil.NodeTypeSort:
    50  		output, err = gen.SortForNode(graph, apiNode)
    51  	case incrutil.NodeTypeReverse:
    52  		output, err = gen.ReverseForNode(graph, apiNode)
    53  	case incrutil.NodeTypeFirst:
    54  		output, err = gen.FirstForNode(graph, apiNode)
    55  	case incrutil.NodeTypeLast:
    56  		output, err = gen.LastForNode(graph, apiNode)
    57  	case incrutil.NodeTypeMin:
    58  		output, err = gen.MinForNode(graph, apiNode)
    59  	case incrutil.NodeTypeMax:
    60  		output, err = gen.MaxForNode(graph, apiNode)
    61  	case incrutil.NodeTypeFetchCSV:
    62  		output, err = gen.FetchCSVForNode(graph, apiNode)
    63  	case incrutil.NodeTypeFetchJSON:
    64  		output, err = gen.FetchJSONForNode(graph, apiNode)
    65  	case incrutil.NodeTypeTableColumn:
    66  		output, err = gen.TableColumnForNode(graph, apiNode)
    67  	case incrutil.NodeTypeTimeseriesChart:
    68  		output, err = gen.TimeseriesChartForNode(graph, apiNode)
    69  	case incrutil.NodeTypeScatterChart:
    70  		output, err = gen.ScatterChartForNode(graph, apiNode)
    71  	case incrutil.NodeTypeCurrentTimestamp:
    72  		output, err = gen.CurrentTimestampForNode(graph, apiNode)
    73  	case incrutil.NodeTypeAlways:
    74  		output, err = gen.AlwaysForNode(graph, apiNode)
    75  	case incrutil.NodeTypeObserver:
    76  		output, err = gen.ObserveForNode(graph, apiNode)
    77  	default:
    78  		return nil, fmt.Errorf("invalid node type %v", apiNode.Metadata.NodeType)
    79  	}
    80  	if err != nil {
    81  		return nil, err
    82  	}
    83  
    84  	if !apiNode.ID.IsZero() {
    85  		incr.ExpertNode(output).SetID(apiNode.ID)
    86  	}
    87  	output.Node().SetLabel(apiNode.Label)
    88  	output.Node().SetMetadata(&apiNode.Metadata)
    89  
    90  	en := incr.ExpertNode(output)
    91  
    92  	en.SetAlways(apiNode.Always)
    93  	en.SetObserver(apiNode.Observer)
    94  	en.SetValid(apiNode.Valid)
    95  	en.SetChangedAt(apiNode.ChangedAt)
    96  	en.SetRecomputedAt(apiNode.RecomputedAt)
    97  	en.SetSetAt(apiNode.SetAt)
    98  	en.SetHeight(apiNode.Height)
    99  	en.SetHeightInRecomputeHeap(apiNode.HeightInRecomputeHeap)
   100  	en.SetHeightInAdjustHeightsHeap(apiNode.HeightInAdjustHeightsHeap)
   101  	return output, nil
   102  }