lang: Functions that build should be copyable

It's not entirely clear if this is required, but it's probably a good
idea. We should consider making it a requirement of the BuildableFunc
interface.
This commit is contained in:
James Shubin
2025-04-19 21:21:19 -04:00
parent 1bb1e056c4
commit 1536a94026
10 changed files with 112 additions and 0 deletions

View File

@@ -354,6 +354,16 @@ func (obj *PrintfFunc) Stream(ctx context.Context) error {
}
}
// Copy is implemented so that the obj.Type value is not lost if we copy this
// function.
func (obj *PrintfFunc) Copy() interfaces.Func {
return &PrintfFunc{
Type: obj.Type, // don't copy because we use this after unification
init: obj.init, // likely gets overwritten anyways
}
}
// Call this function with the input args and return the value if it is possible
// to do so at this time.
func (obj *PrintfFunc) Call(ctx context.Context, args []types.Value) (types.Value, error) {

View File

@@ -393,6 +393,17 @@ func (obj *TemplateFunc) Stream(ctx context.Context) error {
}
}
// Copy is implemented so that the obj.built value is not lost if we copy this
// function.
func (obj *TemplateFunc) Copy() interfaces.Func {
return &TemplateFunc{
Type: obj.Type, // don't copy because we use this after unification
built: obj.built,
init: obj.init, // likely gets overwritten anyways
}
}
// Call this function with the input args and return the value if it is possible
// to do so at this time.
func (obj *TemplateFunc) Call(ctx context.Context, args []types.Value) (types.Value, error) {

View File

@@ -134,6 +134,16 @@ func (obj *HistoryFunc) Build(typ *types.Type) (*types.Type, error) {
return obj.sig(), nil
}
// Copy is implemented so that the type value is not lost if we copy this
// function.
func (obj *HistoryFunc) Copy() interfaces.Func {
return &HistoryFunc{
Type: obj.Type, // don't copy because we use this after unification
init: obj.init, // likely gets overwritten anyways
}
}
// Validate makes sure we've built our struct properly. It is usually unused for
// normal functions that users can use directly.
func (obj *HistoryFunc) Validate() error {

View File

@@ -459,3 +459,13 @@ func (obj *FilterFunc) replaceSubGraph(subgraphInput interfaces.Func) error {
return obj.init.Txn.Commit()
}
// Copy is implemented so that the type value is not lost if we copy this
// function.
func (obj *FilterFunc) Copy() interfaces.Func {
return &FilterFunc{
Type: obj.Type, // don't copy because we use this after unification
init: obj.init, // likely gets overwritten anyways
}
}

View File

@@ -454,3 +454,14 @@ func (obj *MapFunc) replaceSubGraph(subgraphInput interfaces.Func) error {
return obj.init.Txn.Commit()
}
// Copy is implemented so that the type values are not lost if we copy this
// function.
func (obj *MapFunc) Copy() interfaces.Func {
return &MapFunc{
Type: obj.Type, // don't copy because we use this after unification
RType: obj.RType, // don't copy because we use this after unification
init: obj.init, // likely gets overwritten anyways
}
}

View File

@@ -195,6 +195,20 @@ func (obj *LookupFunc) Build(typ *types.Type) (*types.Type, error) {
return obj.fn.Build(typ)
}
// Copy is implemented so that the type value is not lost if we copy this
// function.
func (obj *LookupFunc) Copy() interfaces.Func {
fn := &LookupFunc{
Type: obj.Type, // don't copy because we use this after unification
//init: obj.init, // likely gets overwritten anyways
}
if _, err := fn.Build(obj.Type); err != nil {
// ignore, since we just didn't set the type
}
return fn
}
// Validate tells us if the input struct takes a valid form.
func (obj *LookupFunc) Validate() error {
if obj.fn == nil { // build must be run first

View File

@@ -140,6 +140,21 @@ func (obj *LookupDefaultFunc) Build(typ *types.Type) (*types.Type, error) {
return obj.fn.Build(typ)
}
// Copy is implemented so that the type value is not lost if we copy this
// function.
func (obj *LookupDefaultFunc) Copy() interfaces.Func {
fn := &LookupDefaultFunc{
Type: obj.Type, // don't copy because we use this after unification
//fn: get this through Build()
//init: obj.init, // likely gets overwritten anyways
}
if _, err := fn.Build(obj.Type); err != nil {
// ignore, since we just didn't set the type
}
return fn
}
// Validate tells us if the input struct takes a valid form.
func (obj *LookupDefaultFunc) Validate() error {
if obj.fn == nil { // build must be run first

View File

@@ -181,6 +181,16 @@ func (obj *GetFunc) Build(typ *types.Type) (*types.Type, error) {
return obj.sig(), nil
}
// Copy is implemented so that the type value is not lost if we copy this
// function.
func (obj *GetFunc) Copy() interfaces.Func {
return &GetFunc{
Type: obj.Type, // don't copy because we use this after unification
init: obj.init, // likely gets overwritten anyways
}
}
// Validate makes sure we've built our struct properly. It is usually unused for
// normal functions that users can use directly.
func (obj *GetFunc) Validate() error {

View File

@@ -247,6 +247,17 @@ func (obj *ScheduleFunc) Build(typ *types.Type) (*types.Type, error) {
return obj.sig(), nil
}
// Copy is implemented so that the type value is not lost if we copy this
// function.
func (obj *ScheduleFunc) Copy() interfaces.Func {
return &ScheduleFunc{
Type: obj.Type, // don't copy because we use this after unification
built: obj.built,
init: obj.init, // likely gets overwritten anyways
}
}
// Validate tells us if the input struct takes a valid form.
func (obj *ScheduleFunc) Validate() error {
if !obj.built {

View File

@@ -722,6 +722,16 @@ func (obj *OperatorFunc) Stream(ctx context.Context) error {
}
}
// Copy is implemented so that the obj.Type value is not lost if we copy this
// function.
func (obj *OperatorFunc) Copy() interfaces.Func {
return &OperatorFunc{
Type: obj.Type, // don't copy because we use this after unification
init: obj.init, // likely gets overwritten anyways
}
}
// Call this function with the input args and return the value if it is possible
// to do so at this time.
func (obj *OperatorFunc) Call(ctx context.Context, args []types.Value) (types.Value, error) {