github.com/15mga/kiwi@v0.0.2-0.20240324021231-b95d5c3ac751/loader/loader.go (about)

     1  package loader
     2  
     3  import (
     4  	"github.com/15mga/kiwi"
     5  	"io"
     6  	"net/http"
     7  	"os"
     8  
     9  	"github.com/15mga/kiwi/util"
    10  )
    11  
    12  const (
    13  	LocalLoader = "local"
    14  	HttpLoader  = "http"
    15  )
    16  
    17  var (
    18  	_Default *Loader
    19  )
    20  
    21  func SetDefaultLoader(defaultType string) {
    22  	_Default = NewLoader(defaultType)
    23  }
    24  
    25  func BindParser(assetType string, marshaller util.BytesToAnyErr) {
    26  	_Default.BindParser(assetType, marshaller)
    27  }
    28  
    29  func BindLoader(loaderType string, loader util.StrToBytesErr) {
    30  	_Default.BindLoader(loaderType, loader)
    31  }
    32  
    33  func DefaultLoad(assetType string, action util.FnStrAny, paths ...string) {
    34  	_Default.DefaultLoad(assetType, action, paths...)
    35  }
    36  
    37  func Load(assetType string, loaderType string, action util.FnStrAny, paths ...string) {
    38  	_Default.Load(assetType, loaderType, action, paths...)
    39  }
    40  
    41  func NewLoader(defaultType string) *Loader {
    42  	l := &Loader{
    43  		defaultType:        defaultType,
    44  		assetTypeToParser:  make(map[string]util.BytesToAnyErr),
    45  		loaderTypeToLoader: make(map[string]util.StrToBytesErr),
    46  	}
    47  	l.BindLoader(LocalLoader, func(path string) ([]byte, *util.Err) {
    48  		bytes, err := os.ReadFile(path)
    49  		if err != nil {
    50  			return nil, util.NewErr(util.EcIo, util.M{
    51  				"path":  path,
    52  				"error": err.Error(),
    53  			})
    54  		}
    55  		return bytes, nil
    56  	})
    57  	l.BindLoader(HttpLoader, func(path string) ([]byte, *util.Err) {
    58  		res, err := http.Get(path)
    59  		if err != nil {
    60  			return nil, util.NewErr(util.EcIo, util.M{
    61  				"path":  path,
    62  				"error": err.Error(),
    63  			})
    64  		}
    65  		bytes, err := io.ReadAll(res.Body)
    66  		if err != nil {
    67  			return nil, util.NewErr(util.EcIo, util.M{
    68  				"path":  path,
    69  				"error": err.Error(),
    70  			})
    71  		}
    72  		return bytes, nil
    73  	})
    74  	return l
    75  }
    76  
    77  type Loader struct {
    78  	defaultType        string
    79  	assetTypeToParser  map[string]util.BytesToAnyErr
    80  	loaderTypeToLoader map[string]util.StrToBytesErr
    81  }
    82  
    83  func (l *Loader) BindParser(assetType string, marshaller util.BytesToAnyErr) {
    84  	l.assetTypeToParser[assetType] = marshaller
    85  }
    86  
    87  func (l *Loader) BindLoader(loaderType string, loader util.StrToBytesErr) {
    88  	l.loaderTypeToLoader[loaderType] = loader
    89  }
    90  
    91  func (l *Loader) DefaultLoad(assetType string, action util.FnStrAny, paths ...string) {
    92  	l.Load(assetType, l.defaultType, action, paths...)
    93  }
    94  
    95  func (l *Loader) Load(assetType string, loaderType string, action util.FnStrAny, paths ...string) {
    96  	marshaller, ok := l.assetTypeToParser[assetType]
    97  	if !ok {
    98  		kiwi.Error2(util.EcNotExist, util.M{
    99  			"asset type": assetType,
   100  		})
   101  		return
   102  	}
   103  	loader, ok := l.loaderTypeToLoader[loaderType]
   104  	if !ok {
   105  		kiwi.Error2(util.EcNotExist, util.M{
   106  			"loader type": loaderType,
   107  		})
   108  		return
   109  	}
   110  	for _, path := range paths {
   111  		bytes, e := loader(path)
   112  		if e != nil {
   113  			kiwi.Error(e)
   114  			continue
   115  		}
   116  		o, e := marshaller(bytes)
   117  		if e != nil {
   118  			kiwi.Error(e)
   119  			continue
   120  		}
   121  		action(path, o)
   122  	}
   123  }