github.com/storacha/go-ucanto@v0.7.2/core/schema/options/options.go (about)

     1  package options
     2  
     3  import (
     4  	"github.com/ipfs/go-cid"
     5  	"github.com/ipld/go-ipld-prime/datamodel"
     6  	"github.com/ipld/go-ipld-prime/node/bindnode"
     7  	"github.com/ipld/go-ipld-prime/schema"
     8  )
     9  
    10  // NamedBoolConverter is a generic version of bindnode.NamedBoolConverter
    11  func NamedBoolConverter[T any](typeName schema.TypeName, from func(bool) (T, error), to func(T) (bool, error)) bindnode.Option {
    12  	return bindnode.NamedBoolConverter(typeName, func(b bool) (interface{}, error) {
    13  		t, err := from(b)
    14  		if err != nil {
    15  			return nil, err
    16  		}
    17  		return &t, nil
    18  	}, func(t interface{}) (bool, error) {
    19  		as := t.(*T)
    20  		if as == nil {
    21  			return false, nil
    22  		}
    23  		return to(*as)
    24  	})
    25  }
    26  
    27  // NamedIntConverter is a generic version of bindnode.NamedIntConverter
    28  func NamedIntConverter[T any](typeName schema.TypeName, from func(int64) (T, error), to func(T) (int64, error)) bindnode.Option {
    29  	return bindnode.NamedIntConverter(typeName, func(i int64) (interface{}, error) {
    30  		t, err := from(i)
    31  		if err != nil {
    32  			return nil, err
    33  		}
    34  		return &t, nil
    35  	}, func(t interface{}) (int64, error) {
    36  		as := t.(*T)
    37  		if as == nil {
    38  			return 0, nil
    39  		}
    40  		return to(*as)
    41  	})
    42  }
    43  
    44  // NamedFloatConverter is a generic version of bindnode.NamedFloatConverter
    45  func NamedFloatConverter[T any](typeName schema.TypeName, from func(float64) (T, error), to func(T) (float64, error)) bindnode.Option {
    46  	return bindnode.NamedFloatConverter(typeName, func(f float64) (interface{}, error) {
    47  		t, err := from(f)
    48  		if err != nil {
    49  			return nil, err
    50  		}
    51  		return &t, nil
    52  	}, func(t interface{}) (float64, error) {
    53  		as := t.(*T)
    54  		if as == nil {
    55  			return 0, nil
    56  		}
    57  		return to(*as)
    58  	})
    59  }
    60  
    61  // NamedStringConverter is a generic version of bindnode.NamedStringConverter
    62  func NamedStringConverter[T any](typeName schema.TypeName, from func(string) (T, error), to func(T) (string, error)) bindnode.Option {
    63  	return bindnode.NamedStringConverter(typeName, func(s string) (interface{}, error) {
    64  		t, err := from(s)
    65  		if err != nil {
    66  			return nil, err
    67  		}
    68  		return &t, nil
    69  	}, func(t interface{}) (string, error) {
    70  		as := t.(*T)
    71  		if as == nil {
    72  			return "", nil
    73  		}
    74  		return to(*as)
    75  	})
    76  }
    77  
    78  // NamedBytesConverter is a generic version of bindnode.NamedBytesConverter
    79  func NamedBytesConverter[T any](typeName schema.TypeName, from func([]byte) (T, error), to func(T) ([]byte, error)) bindnode.Option {
    80  	return bindnode.NamedBytesConverter(typeName, func(b []byte) (interface{}, error) {
    81  		t, err := from(b)
    82  		if err != nil {
    83  			return nil, err
    84  		}
    85  		return &t, nil
    86  	}, func(t interface{}) ([]byte, error) {
    87  		as := t.(*T)
    88  		if as == nil {
    89  			return nil, nil
    90  		}
    91  		return to(*as)
    92  	})
    93  }
    94  
    95  // NamedLinkConverter is a generic version of bindnode.NamedLinkConverter
    96  func NamedLinkConverter[T any](typeName schema.TypeName, from func(cid.Cid) (T, error), to func(T) (cid.Cid, error)) bindnode.Option {
    97  	return bindnode.NamedLinkConverter(typeName, func(c cid.Cid) (interface{}, error) {
    98  		t, err := from(c)
    99  		if err != nil {
   100  			return nil, err
   101  		}
   102  		return &t, nil
   103  	}, func(t interface{}) (cid.Cid, error) {
   104  		as := t.(*T)
   105  		if as == nil {
   106  			return cid.Undef, nil
   107  		}
   108  		return to(*as)
   109  	})
   110  }
   111  
   112  // NamedAnyConverter is a generic version of bindnode.NamedAnyConverter
   113  func NamedAnyConverter[T any](typeName schema.TypeName, from func(datamodel.Node) (T, error), to func(T) (datamodel.Node, error)) bindnode.Option {
   114  	return bindnode.NamedAnyConverter(typeName, func(n datamodel.Node) (interface{}, error) {
   115  		t, err := from(n)
   116  		if err != nil {
   117  			return nil, err
   118  		}
   119  		return &t, nil
   120  	}, func(t interface{}) (datamodel.Node, error) {
   121  		as := t.(*T)
   122  		if as == nil {
   123  			return datamodel.Null, nil
   124  		}
   125  		return to(*as)
   126  	})
   127  }