-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.go
54 lines (46 loc) · 1.19 KB
/
decode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package jsonc
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
// CachedDecoder is a managed decoder that caches a copy of json5 transitioned to json
type CachedDecoder struct {
jsonc *Jsonc
ext string
}
// NewCachedDecoder gives a cached decoder
func NewCachedDecoder(ext ...string) *CachedDecoder {
ext = append(ext, ".cached.json")
return &CachedDecoder{New(), ext[0]}
}
// Decode decodes from cache if exists and relevant else decodes from source
func (fd *CachedDecoder) Decode(file string, v interface{}) error {
stat, err := os.Stat(file)
if err != nil {
return err
}
cache := strings.TrimSuffix(file, filepath.Ext(file)) + fd.ext
cstat, err := os.Stat(cache)
exist := !os.IsNotExist(err)
if err != nil && exist {
return err
}
// Update if not exist, or source file modified
update := !exist || stat.ModTime() != cstat.ModTime()
if !update {
jsonb, _ := ioutil.ReadFile(cache)
return json.Unmarshal(jsonb, v)
}
jsonb, _ := ioutil.ReadFile(file)
cfile, err := os.Create(cache)
if err != nil {
return err
}
jsonb = fd.jsonc.Strip(jsonb)
cfile.Write(jsonb)
os.Chtimes(cache, stat.ModTime(), stat.ModTime())
return json.Unmarshal(jsonb, v)
}