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

     1  package example
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"github.com/lmorg/murex/lang"
     7  )
     8  
     9  func init() {
    10  	// Register data-type
    11  	lang.Unmarshallers["example"] = unmarshal
    12  }
    13  
    14  // Describe unmarshaller
    15  func unmarshal(p *lang.Process) (interface{}, error) {
    16  	// Read data from STDIN. Because JSON expects closing tokens, we should
    17  	// read the entire stream before unmarshalling it. For formats like CSV or
    18  	// jsonlines which are more line based, we might want to read STDIN line by
    19  	// line. However given there is just one data return, you still effectively
    20  	// head to read the entire file before returning the structure. There are
    21  	// other APIs for iterative returns for streaming data - more akin to the
    22  	// traditional way UNIX pipes would work.
    23  	b, err := p.Stdin.ReadAll()
    24  	if err != nil {
    25  		return nil, err
    26  	}
    27  
    28  	var v interface{}
    29  	err = json.Unmarshal(b, &v)
    30  
    31  	// Return the Go data structure or error
    32  	return v, err
    33  }