go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/projects/nodes/pkg/funcs/cast.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  
    13  	"go.charczuk.com/projects/nodes/pkg/incrutil"
    14  	"go.charczuk.com/sdk/iter"
    15  )
    16  
    17  func Cast[Input, Output any](ctx context.Context, input Input) (Output, error) {
    18  	return incrutil.CastValue[Output](input)
    19  }
    20  
    21  func CastMany[Input, Output any](ctx context.Context, inputs ...Input) ([]Output, error) {
    22  	return iter.ApplyError(inputs, func(v Input) (Output, error) {
    23  		return incrutil.CastValue[Output](v)
    24  	})
    25  }
    26  
    27  func CastManyMerged[Input, Output any](ctx context.Context, inputs ...[]Input) ([]Output, error) {
    28  	var output []Output
    29  	for _, i := range inputs {
    30  		for _, v := range i {
    31  			vc, err := incrutil.CastValue[Output](v)
    32  			if err != nil {
    33  				return nil, err
    34  			}
    35  			output = append(output, vc)
    36  		}
    37  	}
    38  	return output, nil
    39  }