Fixes
This commit is contained in:
1 parent
04794aea54
commit
cfd31f398c
61 files changed
+2062
-254
No files matched your search
@@ -115,6 +115,25 @@ func (s *Service) ExtractSchema(ctx context.Context, companyID, feedID uuid.UUID
|
||||
out.Fields = fields
|
||||
out.SampleRows = rows
|
||||
out.Preview = buildXMLPreview(data, local)
|
||||
case "json":
|
||||
suggested := guessJSONItemPath(data)
|
||||
out.SuggestedPath = suggested
|
||||
path := itemPathHint
|
||||
if path == "" {
|
||||
path = suggested
|
||||
out.ItemPath = path
|
||||
}
|
||||
fields, rows, err := extractJSONSchema(data, path)
|
||||
if err != nil && !(truncated && rows > 0 && (errors.Is(err, io.ErrUnexpectedEOF) || errors.Is(err, io.EOF) || strings.Contains(strings.ToLower(err.Error()), "unexpected eof"))) {
|
||||
return nil, fmt.Errorf("JSON preview: %w; check the item path and response format", err)
|
||||
}
|
||||
out.Fields = fields
|
||||
out.SampleRows = rows
|
||||
preview := data
|
||||
if len(preview) > 4096 {
|
||||
preview = preview[:4096]
|
||||
}
|
||||
out.Preview = string(preview)
|
||||
default:
|
||||
fields, rows, preview, err := extractCSVSchema(data)
|
||||
if err != nil {
|
||||
@@ -221,6 +240,7 @@ func guessXMLItemPath(data []byte) string {
|
||||
full string
|
||||
}
|
||||
cands := []cand{
|
||||
{"shopitem", "shop/shopitem"},
|
||||
{"item", "rss/channel/item"},
|
||||
{"product", "products/product"},
|
||||
{"entry", "feed/entry"},
|
||||
@@ -232,7 +252,6 @@ func guessXMLItemPath(data []byte) string {
|
||||
if path := findFirstTagPath(data, c.local); path != "" {
|
||||
return path
|
||||
}
|
||||
return c.full
|
||||
}
|
||||
}
|
||||
return "rss/channel/item"
|
||||
@@ -527,3 +546,20 @@ func buildXMLPreview(data []byte, itemLocal string) string {
|
||||
_ = itemLocal
|
||||
return strings.Join(out, "\n")
|
||||
}
|
||||
|
||||
func extractJSONSchema(data []byte, itemPath string) ([]SchemaField, int, error) {
|
||||
acc := map[string]*fieldAcc{}
|
||||
rows := 0
|
||||
_, err := parseJSONItems(bytes.NewReader(data), itemPath, func(row feedRow) error {
|
||||
rows++
|
||||
if rows > schemaMaxRows {
|
||||
return errStopSchema
|
||||
}
|
||||
accumulateRowFields(acc, row)
|
||||
return nil
|
||||
})
|
||||
if err != nil && !errors.Is(err, errStopSchema) {
|
||||
return nil, rows, err
|
||||
}
|
||||
return finalizeSchema(acc), rows, nil
|
||||
}
|
||||
Reference in new issue
Block a user