feat: manually mirror opencoze's code from bytedance
Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
97
backend/infra/impl/document/parser/builtin/parse_json.go
Normal file
97
backend/infra/impl/document/parser/builtin/parse_json.go
Normal file
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package builtin
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/cloudwego/eino/components/document/parser"
|
||||
"github.com/cloudwego/eino/schema"
|
||||
|
||||
contract "github.com/coze-dev/coze-studio/backend/infra/contract/document/parser"
|
||||
)
|
||||
|
||||
func parseJSON(config *contract.Config) parseFn {
|
||||
return func(ctx context.Context, reader io.Reader, opts ...parser.Option) (docs []*schema.Document, err error) {
|
||||
b, err := io.ReadAll(reader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var rawSlices []map[string]string
|
||||
if err = json.Unmarshal(b, &rawSlices); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(rawSlices) == 0 {
|
||||
return nil, fmt.Errorf("[parseJSON] json data is empty")
|
||||
}
|
||||
|
||||
var header []string
|
||||
if config.ParsingStrategy.IsAppend {
|
||||
for _, col := range config.ParsingStrategy.Columns {
|
||||
header = append(header, col.Name)
|
||||
}
|
||||
} else {
|
||||
for k := range rawSlices[0] {
|
||||
// init 取首个 json item 中 key 的随机顺序
|
||||
header = append(header, k)
|
||||
}
|
||||
}
|
||||
|
||||
iter := &jsonIterator{
|
||||
header: header,
|
||||
rows: rawSlices,
|
||||
i: 0,
|
||||
}
|
||||
|
||||
return parseByRowIterator(iter, config, opts...)
|
||||
}
|
||||
}
|
||||
|
||||
type jsonIterator struct {
|
||||
header []string
|
||||
rows []map[string]string
|
||||
i int
|
||||
}
|
||||
|
||||
func (j *jsonIterator) NextRow() (row []string, end bool, err error) {
|
||||
if j.i == 0 {
|
||||
j.i++
|
||||
return j.header, false, nil
|
||||
}
|
||||
|
||||
if j.i == len(j.rows)+1 {
|
||||
return nil, true, nil
|
||||
}
|
||||
|
||||
raw := j.rows[j.i-1]
|
||||
j.i++
|
||||
for _, h := range j.header {
|
||||
val, found := raw[h]
|
||||
if !found {
|
||||
row = append(row, "")
|
||||
} else {
|
||||
row = append(row, val)
|
||||
}
|
||||
}
|
||||
|
||||
return row, false, nil
|
||||
}
|
||||
Reference in New Issue
Block a user