github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/builtins/types/numeric/marshal.go (about)

     1  package numeric
     2  
     3  import (
     4  	"github.com/lmorg/murex/lang"
     5  	"github.com/lmorg/murex/lang/types"
     6  )
     7  
     8  func marshalInt(p *lang.Process, v interface{}) ([]byte, error)    { return marshal(v, types.Integer) }
     9  func marshalFloat(p *lang.Process, v interface{}) ([]byte, error)  { return marshal(v, types.Float) }
    10  func marshalNumber(p *lang.Process, v interface{}) ([]byte, error) { return marshal(v, types.Number) }
    11  
    12  func marshal(v interface{}, dataType string) ([]byte, error) {
    13  	i, err := types.ConvertGoType(v, dataType)
    14  	if err != nil {
    15  		return []byte{'0'}, err
    16  	}
    17  
    18  	s, err := types.ConvertGoType(i, types.String)
    19  	return []byte(s.(string)), err
    20  }
    21  
    22  func unmarshalInt(p *lang.Process) (interface{}, error)    { return unmarshal(p, types.Integer) }
    23  func unmarshalFloat(p *lang.Process) (interface{}, error)  { return unmarshal(p, types.Float) }
    24  func unmarshalNumber(p *lang.Process) (interface{}, error) { return unmarshal(p, types.Number) }
    25  
    26  func unmarshal(p *lang.Process, dataType string) (interface{}, error) {
    27  	b, err := p.Stdin.ReadAll()
    28  	if err != nil {
    29  		return 0, err
    30  	}
    31  
    32  	return types.ConvertGoType(b, dataType)
    33  }