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

     1  package lang
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/lmorg/murex/lang/types"
     8  	"github.com/lmorg/murex/utils/json"
     9  )
    10  
    11  var (
    12  	// ReadIndexes defines the Go functions for the `[ Index ]` murex function
    13  	ReadIndexes = make(map[string]func(*Process, []string) error)
    14  
    15  	// ReadNotIndexes defines the Go functions for the `![ Index ]` murex function
    16  	ReadNotIndexes = make(map[string]func(*Process, []string) error)
    17  
    18  	// Unmarshallers defines the Go functions for converting a murex data type into a Go interface
    19  	Unmarshallers = make(map[string]func(*Process) (interface{}, error))
    20  
    21  	// Marshallers defines the Go functions for converting a Go interface into a murex data type
    22  	Marshallers = make(map[string]func(*Process, interface{}) ([]byte, error))
    23  
    24  	MxInterfaces = make(map[string]MxInterface)
    25  )
    26  
    27  var (
    28  	mimes    = make(map[string]string)
    29  	fileExts = make(map[string]string)
    30  )
    31  
    32  // SetMime defines MIME(s) and assign it a murex data type
    33  func SetMime(dt string, mime ...string) {
    34  	for i := range mime {
    35  		mimes[mime[i]] = dt
    36  	}
    37  }
    38  
    39  // SetFileExtensions defines file extension(s) and assign it a murex data type
    40  func SetFileExtensions(dt string, extension ...string) {
    41  	for i := range extension {
    42  		fileExts[extension[i]] = strings.ToLower(dt)
    43  	}
    44  }
    45  
    46  // WriteMimes takes a JSON-encoded string and writes it to the mimes map.
    47  // This is only intended to be used by `config.Properties.GoFunc.Write()`
    48  func WriteMimes(v interface{}) error {
    49  	switch v := v.(type) {
    50  	case string:
    51  		mimes = make(map[string]string)
    52  		return json.Unmarshal([]byte(v), &mimes)
    53  
    54  	default:
    55  		return fmt.Errorf("invalid data-type. Expecting a %s encoded string", types.Json)
    56  	}
    57  }
    58  
    59  // WriteFileExtensions takes a JSON-encoded string and writes it to the
    60  // fileExts map.
    61  // This is only intended to be used by `config.Properties.GoFunc.Write()`
    62  func WriteFileExtensions(v interface{}) error {
    63  	switch v := v.(type) {
    64  	case string:
    65  		fileExts = make(map[string]string)
    66  		return json.Unmarshal([]byte(v), &fileExts)
    67  
    68  	default:
    69  		return fmt.Errorf("invalid data-type. Expecting a %s encoded string", types.Json)
    70  	}
    71  }