gobuffalo / packr

The simple and easy way to embed static files into Go binaries.
MIT License
3.41k stars 196 forks source link

Packr multi-box support #61

Open FlorentinDUBOIS opened 6 years ago

FlorentinDUBOIS commented 6 years ago

Hello!

I have a case where I have to wrap packr into an array of Box in order to preserve semantic. So, I wonder if it is a good idea to provide a type alias which is an array of box.

type Boxes []Box

This type will implement all methods that a box implements. An example of the usage may be:

package main

import (
    "net/http"

    "github.com/gobuffalo/packr"
)

func main() {
  boxes := packr.NewBoxes("./public", "./dist")

  http.Handle("/", http.FileServer(boxes))
  http.ListenAndServe(":3000", nil)
}

What do you thinks about this feature ?

markbates commented 6 years ago

I like it. I’m wondering is it worth making NewBox accept N number of folders? Or do with this approach? In either case what’s the order of file lookup? FIFO? Or LIFO? Of dist and public both contain foo.txt which one is served up?

FlorentinDUBOIS commented 6 years ago

According to me, a box is associated to a specific folder. So, with this approach an array of box are corresponding to the same number of folders. In addition, I found easier to maintain a type Boxes which an alias of Box because it benefit of all box methods and interfaces implementations.

For me, the order is the one given in parameters of the NewBoxes method. So, a method like MustBytes may have an implementation pretty simple which are easy to maintain, like this:

// MustBytes return the bytes representation of the asset or an error
func (b Boxes) MustBytes(name string) ([]byte, error) {
    for _, box := range b {
        if !box.Has(name) {
            continue
        }

        return box.MustBytes(name)
    }

    return nil, fmt.Errorf("No asset found for: %s", name)
}

With this way to manage box, if some boxes contains the same file name, it is the box which is declare in first position that will be used.

This is my way to manage boxes. What do you thinks about it ? Do you agree ? Have you some points you thinks that it will be difficult to implements ?