|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + |
| 9 | + "github.com/sumup/acp/feed" |
| 10 | +) |
| 11 | + |
| 12 | +func main() { |
| 13 | + products := sampleProducts() |
| 14 | + productFeed := feed.New(products) |
| 15 | + |
| 16 | + outDir := filepath.Join("examples", "feed", "output") |
| 17 | + if err := os.MkdirAll(outDir, 0o755); err != nil { |
| 18 | + log.Fatalf("create output directory: %v", err) |
| 19 | + } |
| 20 | + |
| 21 | + jsonlPath := filepath.Join(outDir, "product_feed.jsonl.gz") |
| 22 | + if err := writeJSONL(productFeed, jsonlPath); err != nil { |
| 23 | + log.Fatalf("write jsonl feed: %v", err) |
| 24 | + } |
| 25 | + |
| 26 | + csvPath := filepath.Join(outDir, "product_feed.csv.gz") |
| 27 | + if err := writeCSV(productFeed, csvPath); err != nil { |
| 28 | + log.Fatalf("write csv feed: %v", err) |
| 29 | + } |
| 30 | + |
| 31 | + log.Printf("wrote %s", jsonlPath) |
| 32 | + log.Printf("wrote %s", csvPath) |
| 33 | +} |
| 34 | + |
| 35 | +func writeJSONL(productFeed feed.Feed, path string) error { |
| 36 | + file, err := os.Create(path) |
| 37 | + if err != nil { |
| 38 | + return fmt.Errorf("create file: %w", err) |
| 39 | + } |
| 40 | + defer func() { |
| 41 | + _ = file.Close() |
| 42 | + }() |
| 43 | + |
| 44 | + return productFeed.WriteJSONLGz(file) |
| 45 | +} |
| 46 | + |
| 47 | +func writeCSV(productFeed feed.Feed, path string) error { |
| 48 | + file, err := os.Create(path) |
| 49 | + if err != nil { |
| 50 | + return fmt.Errorf("create file: %w", err) |
| 51 | + } |
| 52 | + defer func() { |
| 53 | + _ = file.Close() |
| 54 | + }() |
| 55 | + |
| 56 | + return productFeed.WriteCSVGz(file) |
| 57 | +} |
| 58 | + |
| 59 | +func sampleProducts() []feed.Product { |
| 60 | + return []feed.Product{ |
| 61 | + { |
| 62 | + EnableSearch: true, |
| 63 | + EnableCheckout: true, |
| 64 | + ID: "sku_latte_12oz", |
| 65 | + Title: "Oat Milk Latte (12oz)", |
| 66 | + Description: "Smooth espresso with steamed oat milk.", |
| 67 | + Link: "https://store.example.com/products/sku_latte_12oz", |
| 68 | + ProductCategory: "Food, Beverages & Tobacco > Beverages > Coffee", |
| 69 | + ImageLink: "https://store.example.com/images/sku_latte_12oz.jpg", |
| 70 | + Price: "6.50 USD", |
| 71 | + Availability: "in_stock", |
| 72 | + SellerName: "Example Roasters", |
| 73 | + SellerURL: "https://store.example.com", |
| 74 | + InventoryQuantity: intPtr(120), |
| 75 | + Shipping: []string{ |
| 76 | + "US:CA:Ground:5.00 USD", |
| 77 | + "US:NY:Ground:6.00 USD", |
| 78 | + }, |
| 79 | + PickupMethod: "in_store", |
| 80 | + PickupSLA: "1 day", |
| 81 | + }, |
| 82 | + { |
| 83 | + EnableSearch: true, |
| 84 | + EnableCheckout: true, |
| 85 | + ID: "sku_mug_slate", |
| 86 | + Title: "Stoneware Mug (Slate)", |
| 87 | + Description: "12oz stoneware mug with matte finish.", |
| 88 | + Link: "https://store.example.com/products/sku_mug_slate", |
| 89 | + ProductCategory: "Home & Garden > Kitchen & Dining > Drinkware", |
| 90 | + ImageLink: "https://store.example.com/images/sku_mug_slate.jpg", |
| 91 | + AdditionalImageLink: []string{ |
| 92 | + "https://store.example.com/images/sku_mug_slate_alt1.jpg", |
| 93 | + "https://store.example.com/images/sku_mug_slate_alt2.jpg", |
| 94 | + }, |
| 95 | + Price: "15.00 USD", |
| 96 | + SalePrice: "12.00 USD", |
| 97 | + Availability: "in_stock", |
| 98 | + Condition: feed.ProductConditionNew, |
| 99 | + Brand: "Example Roasters", |
| 100 | + Material: "Stoneware", |
| 101 | + Dimensions: "4x4x4 in", |
| 102 | + Shipping: []string{ |
| 103 | + "US:::7.00 USD", |
| 104 | + }, |
| 105 | + SellerName: "Example Roasters", |
| 106 | + SellerURL: "https://store.example.com", |
| 107 | + }, |
| 108 | + { |
| 109 | + EnableSearch: true, |
| 110 | + EnableCheckout: true, |
| 111 | + ID: "sku_shirt_black_s", |
| 112 | + Title: "Logo T-Shirt (Black, S)", |
| 113 | + Description: "Soft cotton tee with embroidered logo.", |
| 114 | + Link: "https://store.example.com/products/sku_shirt_black_s", |
| 115 | + ProductCategory: "Apparel & Accessories > Clothing", |
| 116 | + ImageLink: "https://store.example.com/images/sku_shirt_black.jpg", |
| 117 | + Price: "24.00 USD", |
| 118 | + Availability: "in_stock", |
| 119 | + ItemGroupID: "sku_shirt_black", |
| 120 | + ItemGroupTitle: "Logo T-Shirt (Black)", |
| 121 | + Color: "Black", |
| 122 | + Size: "S", |
| 123 | + SizeSystem: "US", |
| 124 | + Gender: "unisex", |
| 125 | + SellerName: "Example Roasters", |
| 126 | + SellerURL: "https://store.example.com", |
| 127 | + }, |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func intPtr(v int) *int { |
| 132 | + return &v |
| 133 | +} |
0 commit comments