wip: 重构基本完成, 还未测试
This commit is contained in:
@@ -2,8 +2,10 @@ package xfile
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/loveuer/esgo2dump/internal/opt"
|
||||
@@ -17,8 +19,7 @@ type client struct {
|
||||
scanner *bufio.Scanner
|
||||
}
|
||||
|
||||
// Read implements model.IO.
|
||||
func (c *client) ReadData(limit int, query map[string]any, fields []string, sort []string) ([]map[string]any, error) {
|
||||
func (c *client) ReadData(ctx context.Context, limit int, query map[string]any, fields []string, sort []string) ([]map[string]any, error) {
|
||||
if len(query) != 0 {
|
||||
return nil, fmt.Errorf("file with query is unsupported")
|
||||
}
|
||||
@@ -51,8 +52,7 @@ func (c *client) ReadData(limit int, query map[string]any, fields []string, sort
|
||||
return list, nil
|
||||
}
|
||||
|
||||
// Write implements model.IO.
|
||||
func (c *client) WriteData(items []map[string]any) (int, error) {
|
||||
func (c *client) WriteData(ctx context.Context, items []map[string]any) (int, error) {
|
||||
total := 0
|
||||
for _, item := range items {
|
||||
bs, err := json.Marshal(item)
|
||||
@@ -74,6 +74,66 @@ func (c *client) WriteData(items []map[string]any) (int, error) {
|
||||
return total, nil
|
||||
}
|
||||
|
||||
func (c *client) ReadMapping(ctx context.Context) (map[string]any, error) {
|
||||
var (
|
||||
err error
|
||||
bs []byte
|
||||
)
|
||||
|
||||
if bs, err = io.ReadAll(c.f); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
m := make(map[string]any)
|
||||
|
||||
if err = json.Unmarshal(bs, &m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (c *client) WriteMapping(ctx context.Context, mapping map[string]any) error {
|
||||
bs, err := json.Marshal(mapping)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = c.f.Write(bs)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *client) ReadSetting(ctx context.Context) (map[string]any, error) {
|
||||
var (
|
||||
err error
|
||||
bs []byte
|
||||
)
|
||||
|
||||
if bs, err = io.ReadAll(c.f); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
m := make(map[string]any)
|
||||
|
||||
if err = json.Unmarshal(bs, &m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (c *client) WriteSetting(ctx context.Context, setting map[string]any) error {
|
||||
bs, err := json.Marshal(setting)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = c.f.Write(bs)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func NewClient(path string, t model.IOType) (model.IO[map[string]any], error) {
|
||||
var (
|
||||
info os.FileInfo
|
||||
|
||||
Reference in New Issue
Block a user