Fixes
This commit is contained in:
1 parent
04794aea54
commit
cfd31f398c
61 files changed
+2062
-254
No files matched your search
@@ -31,18 +31,23 @@ type feedRow map[string]string
|
||||
|
||||
func detectFeedFormat(feedType, contentType, urlHint string, sample []byte) string {
|
||||
ft := strings.ToLower(strings.TrimSpace(feedType))
|
||||
if ft == "csv" || ft == "xml" {
|
||||
if ft == "csv" || ft == "xml" || ft == "json" {
|
||||
return ft
|
||||
}
|
||||
ct := strings.ToLower(contentType)
|
||||
u := strings.ToLower(urlHint)
|
||||
switch {
|
||||
case strings.Contains(ct, "json") || strings.Contains(u, ".json") || strings.Contains(u, "format=json"):
|
||||
return "json"
|
||||
case strings.Contains(ct, "csv") || strings.HasSuffix(u, ".csv"):
|
||||
return "csv"
|
||||
case strings.Contains(ct, "xml") || strings.HasSuffix(u, ".xml"):
|
||||
return "xml"
|
||||
}
|
||||
trimmed := bytes.TrimSpace(sample)
|
||||
if len(trimmed) > 0 && (trimmed[0] == '{' || trimmed[0] == '[') {
|
||||
return "json"
|
||||
}
|
||||
if len(trimmed) > 0 && trimmed[0] == '<' {
|
||||
return "xml"
|
||||
}
|
||||
@@ -173,7 +178,7 @@ func guessXMLItemLocal(data []byte) string {
|
||||
sample = sample[:xmlItemGuessBytes]
|
||||
}
|
||||
lower := strings.ToLower(sample)
|
||||
for _, cand := range []string{"item", "product", "entry", "offer", "row"} {
|
||||
for _, cand := range []string{"shopitem", "item", "entry", "offer", "row", "product"} {
|
||||
if strings.Contains(lower, "<"+cand) || strings.Contains(lower, ":"+cand) {
|
||||
return cand
|
||||
}
|
||||
@@ -193,6 +198,7 @@ func readXMLElementMap(dec *xml.Decoder, start xml.StartElement) (feedRow, error
|
||||
row[start.Name.Local+"/"+key] = a.Value
|
||||
}
|
||||
var path []string
|
||||
var paramName, paramValue string
|
||||
for {
|
||||
tok, err := dec.Token()
|
||||
if err != nil {
|
||||
@@ -207,7 +213,38 @@ func readXMLElementMap(dec *xml.Decoder, start xml.StartElement) (feedRow, error
|
||||
row[key] = a.Value
|
||||
}
|
||||
case xml.EndElement:
|
||||
if strings.EqualFold(t.Name.Local, "PARAM") && len(path) >= 2 && strings.EqualFold(path[len(path)-2], "PARAMS") {
|
||||
name := strings.TrimSpace(paramName)
|
||||
value := strings.TrimSpace(paramValue)
|
||||
if name != "" && value != "" {
|
||||
key := "PARAMS/" + sanitizePathSegment(name)
|
||||
if key != "PARAMS/" {
|
||||
row[key] = value
|
||||
}
|
||||
}
|
||||
paramName, paramValue = "", ""
|
||||
}
|
||||
if len(path) == 0 {
|
||||
count := 0
|
||||
for k := range row {
|
||||
if strings.HasPrefix(k, "PARAMS/") && !strings.Contains(strings.TrimPrefix(k, "PARAMS/"), "/") {
|
||||
count++
|
||||
}
|
||||
}
|
||||
if count > 0 {
|
||||
row["PARAMS"] = fmt.Sprintf("%d named attributes", count)
|
||||
}
|
||||
// Supplier feeds often publish additional images as numbered siblings.
|
||||
images := make([]string, 0, 12)
|
||||
for i := 1; i <= 50; i++ {
|
||||
name := fmt.Sprintf("IMGURL_ALTERNATIVE_%02d", i)
|
||||
if v := strings.TrimSpace(row[name]); v != "" {
|
||||
images = append(images, v)
|
||||
}
|
||||
}
|
||||
if len(images) > 0 {
|
||||
row["ADDITIONAL_IMAGE_URLS"] = strings.Join(images, ",")
|
||||
}
|
||||
return row, nil
|
||||
}
|
||||
path = path[:len(path)-1]
|
||||
@@ -217,6 +254,19 @@ func readXMLElementMap(dec *xml.Decoder, start xml.StartElement) (feedRow, error
|
||||
continue
|
||||
}
|
||||
leaf := path[len(path)-1]
|
||||
if len(path) >= 3 && strings.EqualFold(path[len(path)-3], "PARAMS") && strings.EqualFold(path[len(path)-2], "PARAM") {
|
||||
if strings.EqualFold(leaf, "PARAM_NAME") {
|
||||
paramName += text
|
||||
continue
|
||||
}
|
||||
if strings.EqualFold(leaf, "VAL") {
|
||||
paramValue += text
|
||||
continue
|
||||
}
|
||||
if strings.EqualFold(leaf, "ORDER") {
|
||||
continue
|
||||
}
|
||||
}
|
||||
full := strings.Join(path, "/")
|
||||
// Prefer nested path as source of truth; only set bare leaf when
|
||||
// unique (no other nested field already owns this leaf name).
|
||||
|
||||
Reference in new issue
Block a user