Skip to content

Commit 46bd79c

Browse files
authored
fix: fixes retrocompatibility with namespaces by using fs.Register (#105)
* fix: fixes retrocompatibility with namespaces by using fs.Register instead on fs.RegisterWithNamespace when the namespace is default. * chore: tweaks the default namespace check.
1 parent 0c7347a commit 46bd79c

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

example/statik/statik.go

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fs/fs.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,18 @@ type statikFS struct {
4545
dirs map[string][]string
4646
}
4747

48+
const defaultNamespace = "default"
49+
50+
// IsDefaultNamespace returns true if the assetNamespace is
51+
// the default one
52+
func IsDefaultNamespace(assetNamespace string) bool {
53+
return assetNamespace == defaultNamespace
54+
}
55+
4856
// Register registers zip contents data, later used to initialize
4957
// the statik file system.
5058
func Register(data string) {
51-
RegisterWithNamespace("default", data)
59+
RegisterWithNamespace(defaultNamespace, data)
5260
}
5361

5462
// RegisterWithNamespace registers zip contents data and set asset namespace,
@@ -60,7 +68,7 @@ func RegisterWithNamespace(assetNamespace string, data string) {
6068
// New creates a new file system with the default registered zip contents data.
6169
// It unzips all files and stores them in an in-memory map.
6270
func New() (http.FileSystem, error) {
63-
return NewWithNamespace("default")
71+
return NewWithNamespace(defaultNamespace)
6472
}
6573

6674
// NewWithNamespace creates a new file system with the registered zip contents data.

statik.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import (
3030
"strings"
3131
"time"
3232
"unicode"
33+
34+
"github.com/rakyll/statik/fs"
3335
)
3436

3537
const nameSourceFile = "statik.go"
@@ -279,7 +281,7 @@ import (
279281
)
280282
281283
`, tags, comment, namePackage)
282-
if assetNamespace != "default" {
284+
if !fs.IsDefaultNamespace(assetNamespace) {
283285
fmt.Fprintf(&qb, `
284286
const %s = "%s" // static asset namespace
285287
`, assetNamespaceIdentify, assetNamespace)
@@ -288,10 +290,18 @@ const %s = "%s" // static asset namespace
288290
func init() {
289291
data := "`)
290292
FprintZipData(&qb, buffer.Bytes())
291-
fmt.Fprintf(&qb, `"
292-
fs.RegisterWithNamespace("%s", data)
293-
}
294-
`, assetNamespace)
293+
if fs.IsDefaultNamespace(assetNamespace) {
294+
fmt.Fprint(&qb, `"
295+
fs.Register(data)
296+
}
297+
`)
298+
299+
} else {
300+
fmt.Fprintf(&qb, `"
301+
fs.RegisterWithNamespace("%s", data)
302+
}
303+
`, assetNamespace)
304+
}
295305

296306
if err = ioutil.WriteFile(f.Name(), qb.Bytes(), 0644); err != nil {
297307
return

0 commit comments

Comments
 (0)