snapshot of https://github.com/cortezaproject/corteza.git (2024.9.x, 2026-09-18; blobs > 131072B omitted)
Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
This commit is contained in:
170
server/pkg/envoyx/json/decoder.go
Normal file
170
server/pkg/envoyx/json/decoder.go
Normal file
@@ -0,0 +1,170 @@
|
||||
package json
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/cortezaproject/corteza/server/pkg/envoyx/datasource"
|
||||
"github.com/gabriel-vasile/mimetype"
|
||||
)
|
||||
|
||||
type (
|
||||
decoder struct {
|
||||
ident string
|
||||
|
||||
src *os.File
|
||||
reader *json.Decoder
|
||||
|
||||
header []string
|
||||
count uint64
|
||||
}
|
||||
)
|
||||
|
||||
func CanDecodeFile(f io.Reader) bool {
|
||||
m, err := mimetype.DetectReader(f)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return CanDecodeExt(m.Extension())
|
||||
}
|
||||
|
||||
func CanDecodeMime(m string) bool {
|
||||
return m == "application/json" || m == "application/jsonlines"
|
||||
}
|
||||
|
||||
func CanDecodeExt(ext string) bool {
|
||||
pt := strings.Split(ext, ".")
|
||||
ext = strings.TrimSpace(pt[len(pt)-1])
|
||||
return ext == "jsonl" || ext == "json" || ext == "ndjson"
|
||||
}
|
||||
|
||||
// Decoder inits a new csv decoder from the given reader
|
||||
//
|
||||
// @todo hold small files in mem to avoid needles disc access
|
||||
func Decoder(r io.Reader, ident string) (out *decoder, err error) {
|
||||
out = &decoder{
|
||||
ident: ident,
|
||||
}
|
||||
|
||||
out.src, err = ioutil.TempFile(os.TempDir(), "*.ndjson")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = out.flushTemp(r)
|
||||
defer out.src.Seek(0, 0)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
out.reader = json.NewDecoder(out.src)
|
||||
|
||||
seenHeader := make(map[string]bool)
|
||||
|
||||
var aux datasource.RawRecord
|
||||
for out.reader.More() {
|
||||
err = out.reader.Decode(&aux)
|
||||
if err == io.EOF {
|
||||
return out, nil
|
||||
} else if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for f := range aux {
|
||||
if seenHeader[f] {
|
||||
continue
|
||||
}
|
||||
seenHeader[f] = true
|
||||
|
||||
out.header = append(out.header, f)
|
||||
}
|
||||
|
||||
// Entry count
|
||||
out.count++
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (d *decoder) SetConfigs(config map[string]any) (err error) {
|
||||
// @todo when needed
|
||||
return
|
||||
}
|
||||
|
||||
// Cleanup should be called before we stop using the decoder
|
||||
func (d *decoder) Cleanup() error {
|
||||
return os.Remove(d.src.Name())
|
||||
}
|
||||
|
||||
// SetIdent overwrites the system defined identifier
|
||||
func (d *decoder) SetIdent(ident string) {
|
||||
d.ident = ident
|
||||
}
|
||||
|
||||
// Ident returns the assigned identifier
|
||||
func (d *decoder) Ident() string {
|
||||
return d.ident
|
||||
}
|
||||
|
||||
// Fields returns every available field in this dataset
|
||||
func (d *decoder) Fields() []string {
|
||||
return d.header
|
||||
}
|
||||
|
||||
// Reset resets the decoder to the start
|
||||
func (d *decoder) Reset(_ context.Context) error {
|
||||
_, err := d.src.Seek(0, 0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
d.reader = json.NewDecoder(d.src)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Next returns the field: value mapping for the next row
|
||||
func (d *decoder) Next(_ context.Context, out datasource.RawRecord) (more bool, err error) {
|
||||
tmp := make(datasource.RawRecord)
|
||||
err = d.reader.Decode(&tmp)
|
||||
|
||||
if err == io.EOF {
|
||||
return false, nil
|
||||
} else if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
// Empty out missing fields to keep consistent with CSV
|
||||
for _, h := range d.header {
|
||||
if _, ok := tmp[h]; !ok {
|
||||
vv := tmp[h]
|
||||
vv.Values = make([]string, 0)
|
||||
tmp[h] = vv
|
||||
}
|
||||
}
|
||||
|
||||
for k, v := range tmp {
|
||||
out[k] = v
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// Count returns the total number of rows in the dataset
|
||||
func (d *decoder) Count() uint64 {
|
||||
return d.count
|
||||
}
|
||||
|
||||
func (d *decoder) flushTemp(r io.Reader) (err error) {
|
||||
_, err = io.Copy(d.src, r)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
d.src.Seek(0, 0)
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user