github.com/blend/go-sdk@v1.20240719.1/shardutil/invocation_option.go (about)

     1  /*
     2  
     3  Copyright (c) 2024 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package shardutil
     9  
    10  import (
    11  	"database/sql"
    12  	"fmt"
    13  
    14  	"github.com/blend/go-sdk/db"
    15  )
    16  
    17  // InvocationOption is an option that returns an invocation option based on a partition index.
    18  type InvocationOption func(partitionIndex int) db.InvocationOption
    19  
    20  // OptTxs returns a base manager invocation option that
    21  // parameterizes the transaction per invocation based on an array of transactions.
    22  func OptTxs(txns ...*sql.Tx) InvocationOption {
    23  	return func(partitionIndex int) db.InvocationOption {
    24  		return func(i *db.Invocation) {
    25  			db.OptTx(txns[partitionIndex])(i)
    26  		}
    27  	}
    28  }
    29  
    30  // OptLabel sets a label for invocations.
    31  func OptLabel(label string) InvocationOption {
    32  	return func(_ int) db.InvocationOption { return db.OptLabel(label) }
    33  }
    34  
    35  // OptPartitionLabel sets a label for invocations.
    36  func OptPartitionLabel(label string) InvocationOption {
    37  	return func(partitionIndex int) db.InvocationOption {
    38  		return db.OptLabel(fmt.Sprintf("%s_%d", label, partitionIndex))
    39  	}
    40  }