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

     1  package paths
     2  
     3  import (
     4  	"fmt"
     5  	gopath "path"
     6  
     7  	"github.com/lmorg/murex/lang"
     8  	"github.com/lmorg/murex/lang/types"
     9  	"github.com/lmorg/murex/utils/path"
    10  )
    11  
    12  func init() {
    13  	lang.MxInterfaces[types.Path] = new(mxiPath)
    14  }
    15  
    16  type mxiPath struct {
    17  	path string
    18  }
    19  
    20  func (mxi mxiPath) New(path string) (lang.MxInterface, error) {
    21  	return &mxiPath{path}, nil
    22  }
    23  
    24  func (mxi *mxiPath) GetValue() interface{} {
    25  	v, _ := path.Unmarshal([]byte(mxi.path))
    26  	return v
    27  }
    28  
    29  func (mxi *mxiPath) GetString() string {
    30  	return gopath.Clean(mxi.path)
    31  }
    32  
    33  func (mxi *mxiPath) Set(v interface{}, changePath []string) error {
    34  	switch t := v.(type) {
    35  	case string:
    36  		mxi.path = t
    37  	case []byte:
    38  		mxi.path = string(t)
    39  	default:
    40  		if len(changePath) == 2 &&
    41  			(changePath[1] == path.EXISTS || changePath[1] == path.IS_DIR || changePath[1] == path.IS_RELATIVE) {
    42  			return fmt.Errorf("'%s' is a read only property", changePath[1])
    43  		}
    44  
    45  		b, err := path.Marshal(v)
    46  		if err != nil {
    47  			return err
    48  		}
    49  		mxi.path = string(b)
    50  	}
    51  	return nil
    52  }