go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/projects/nodes/pkg/funcs/table_column.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 funcs
     9  
    10  import (
    11  	"context"
    12  	"fmt"
    13  
    14  	"go.charczuk.com/projects/nodes/pkg/incrutil"
    15  	"go.charczuk.com/projects/nodes/pkg/types"
    16  )
    17  
    18  // TableColumn selects the values from a given column by index.
    19  func TableColumn[Output any](_ context.Context, columnIndex int64, input *types.Table) (output Output, err error) {
    20  	if columnIndex < 0 {
    21  		err = fmt.Errorf("table column; column invalid: %d", columnIndex)
    22  		return
    23  	}
    24  	if input == nil {
    25  		return
    26  	}
    27  	if int(columnIndex) >= len(input.Columns) {
    28  		err = fmt.Errorf("table column; column index out of range: %d", columnIndex)
    29  		return
    30  	}
    31  	if input == nil {
    32  		err = fmt.Errorf("table column; input table is unset")
    33  		return
    34  	}
    35  	column := input.Columns[columnIndex]
    36  	output, err = incrutil.CastValue[Output](column.Values)
    37  	return
    38  }