add export as yml_catalog

This commit is contained in:
Aleksandr Trushkin
2024-01-26 19:34:47 +03:00
parent a1e767217b
commit a0b36ba83d
7 changed files with 371 additions and 57 deletions

View File

@ -0,0 +1,61 @@
package export
import "time"
type Param struct {
Name string `xml:"name,attr"`
Value string `xml:",chardata"`
}
type Offer struct {
ID int64 `xml:"id,attr"`
Type string `xml:"type,attr"`
Available bool `xml:"available,attr"`
URL string `xml:"url"`
Price int `xml:"price"`
CurrencyID string `xml:"currencyId"`
CategoryID int64 `xml:"categoryId"`
PictureURLs []string `xml:"picture"`
Vendor string `xml:"vendor"`
Model string `xml:"model"`
VendorCode int `xml:"vendorCode"`
TypePrefix string `xml:"typePrefix"`
Description string `xml:"description"`
ManufacturerWarrany bool `xml:"manufacturer_warranty"`
Params []Param `xml:"param"`
}
type Currency struct {
ID string `xml:"id,attr"` // RUR only
Rate int64 `xml:"rate,attr"` // 1?
}
type Category struct {
ID int64 `xml:"id,attr"`
ParentID int64 `xml:"parent_id,attr,omiempty"`
Name string `xml:",chardata"`
}
type Shop struct {
Name string `xml:"name"` // r
Company string `xml:"company"` // r
URL string `xml:"url"` // r
Platform string `xml:"platform"`
Version string `xml:"version"`
Currencies []Currency `xml:"currencies"` // r RUR only
Categories []Category `xml:"categories>category"` // r
Offers []Offer `xml:"offer"` // r
}
type YmlContainer struct {
XMLName struct{} `xml:"yml_catalog"`
YmlCatalog
}
type YmlCatalog struct {
Date time.Time `xml:"date,attr"`
Shop Shop `xml:"shop"`
}

View File

@ -0,0 +1,81 @@
package export
import (
"encoding/xml"
"os"
"testing"
"github.com/brianvoe/gofakeit/v6"
)
func TestYMLSerialize(t *testing.T) {
faker := gofakeit.New(0)
categories := make([]Category, faker.Rand.Intn(4))
knownCategory := map[int]struct{}{}
categoryIDs := make([]int, 0, 10)
for i := range categories {
categories[i].ID = faker.Rand.Int()
categories[i].Name = faker.HipsterWord()
categories[i].ParentID = faker.Rand.Int()
if _, ok := knownCategory[categories[i].ID]; ok {
continue
}
knownCategory[categories[i].ID] = struct{}{}
categoryIDs = append(categoryIDs, categories[i].ID)
}
offers := make([]Offer, faker.Rand.Intn(5)+1)
for i := range offers {
offer := &offers[i]
offer.ID = faker.Int64()
offer.Type = "vendor.model"
offer.Available = true
offer.URL = faker.URL()
offer.Price = int(faker.Price(10, 1000))
offer.CurrencyID = "RUR"
offer.CategoryID = categoryIDs[faker.Rand.Intn(len(categoryIDs))]
for i := 0; i < faker.Rand.Intn(3); i++ {
offer.PictureURLs = append(offer.PictureURLs, faker.ImageURL(128, 128))
}
offer.Vendor = faker.Company()
offer.Model = faker.CarModel()
offer.VendorCode = faker.Rand.Int()
offer.TypePrefix = faker.ProductName()
offer.Description = faker.Sentence(12)
offer.ManufacturerWarrany = true
for i := 0; i < faker.Rand.Intn(8); i++ {
offer.Params = append(offer.Params, Param{
Name: faker.AdjectiveProper(),
Value: faker.Digit(),
})
}
}
var catalog YmlCatalog
catalog.Shop = Shop{
Name: faker.ProductName(),
URL: faker.URL(),
Company: faker.Company(),
Platform: "BSM/Yandex/Market",
Version: faker.AppVersion(),
Currencies: []Currency{{
ID: "RUR",
Rate: 1,
}},
Categories: categories,
Offers: offers,
}
catalog.Date = faker.Date()
container := YmlContainer{
YmlCatalog: catalog,
}
enc := xml.NewEncoder(os.Stdout)
enc.Indent("", " ")
_ = enc.Encode(container)
println()
t.FailNow()
}