photoprism/pkg/clusters/json_importer.go
Eng Zer Jun 44f7700c0c
Enable module graph pruning and deprecate io/ioutil (#1600)
* Backend: Enable Go module graph pruning and lazy module loading

This commit applies the changes by running `go mod tidy -go=1.17` to
enable module graph pruning and lazy module loading supported by Go 1.17
or higher.

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* Backend: Move from io/ioutil to io and os package

The io/ioutil package has been deprecated as of Go 1.16, see
https://golang.org/doc/go1.16#ioutil. This commit replaces the existing
io/ioutil functions with their new definitions in io and os packages.

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
2021-10-06 07:10:50 +02:00

53 lines
758 B
Go

package clusters
import (
"encoding/json"
"os"
)
type jsonImporter struct {
}
func JsonImporter() Importer {
return &jsonImporter{}
}
func (i *jsonImporter) Import(file string, start, end int) ([][]float64, error) {
if start < 0 || end < 0 || start > end {
return [][]float64{}, errInvalidRange
}
f, err := os.ReadFile(file)
if err != nil {
return [][]float64{}, err
}
var (
d = make([][]float64, 0)
s = end - start + 1
g = make([]float64, 0, s)
c int
)
err = json.Unmarshal(f, &d)
if err != nil {
return [][]float64{}, err
}
for i, _ := range d {
c = 0
for j := start; j <= end; j++ {
g[c] = d[i][j]
c++
}
d[i] = make([]float64, 0, s)
for j := 0; j < s; j++ {
d[i][j] = g[j]
}
}
return d, nil
}