github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/lang/define_marshal.go (about)

     1  package lang
     2  
     3  import (
     4  	"errors"
     5  )
     6  
     7  // MarshalData is a global marshaller which should be called from within murex
     8  // builtin commands (etc).
     9  // See docs/apis/marshaldata.md for more details
    10  func MarshalData(p *Process, dataType string, data interface{}) (b []byte, err error) {
    11  	// This is one of the very few maps in Murex which isn't hidden behind a sync
    12  	// lock of one description or other. The rational is that even mutexes can
    13  	// add a noticeable overhead on the performance of tight loops and I expect
    14  	// this function to be called _a lot_ while also only needing to be written
    15  	// to via code residing in within builtin types init() function (ie while
    16  	// murex is effectively single threaded). So there shouldn't be any data-
    17  	// races -- PROVIDING developers strictly follow the pattern of only writing
    18  	// to this map within init() func's.
    19  	if Marshallers[dataType] == nil {
    20  		return nil, errors.New("I don't know how to marshal `" + dataType + "`.")
    21  	}
    22  
    23  	b, err = Marshallers[dataType](p, data)
    24  	if err != nil {
    25  		return nil, errors.New("[" + dataType + " marshaller] " + err.Error())
    26  	}
    27  
    28  	return
    29  }