Skip to content

Commit

Permalink
Re-encode JSON for storage. Kinda yuck.
Browse files Browse the repository at this point in the history
  • Loading branch information
tjhowse committed Aug 7, 2024
1 parent cf1de98 commit 7384f33
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
4 changes: 1 addition & 3 deletions internal/woolworths/woolworths_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,6 @@ func (w *Woolworths) saveProductInfoExtended(tx *sql.Tx, productInfo woolworthsP
var err error
var result sql.Result

productInfoString := "raw json todo"

result, err = tx.Exec(`
INSERT INTO products (productID, name, description, priceCents, weightGrams, productJSON, updated)
VALUES (?, ?, ?, ?, ?, ?, ?)
Expand All @@ -156,7 +154,7 @@ func (w *Woolworths) saveProductInfoExtended(tx *sql.Tx, productInfo woolworthsP
updated = excluded.updated`,
productInfo.ID, productInfo.Info.DisplayName, productInfo.Info.Description,
productInfo.Info.Price.Mul(decimal.NewFromInt(100)).IntPart(),
productInfo.Info.UnitWeightInGrams, productInfoString, productInfo.Updated)
productInfo.Info.UnitWeightInGrams, productInfo.RawJSON, productInfo.Updated)

if err != nil {
return fmt.Errorf("failed to update product info: %w", err)
Expand Down
8 changes: 8 additions & 0 deletions internal/woolworths/woolworths_web.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,18 @@ func (w *Woolworths) extractProductInfoFromProductListPage(body []byte) ([]woolw
return productInfos, fmt.Errorf("expected 1 product in bundle, got %d", len(products.Products))
}
product := products.Products[0]
// Not entirely happy about this, but I don't see a better way of getting the raw JSON
// of just the product into the info struct.
encoded, err := json.Marshal(product)
if err != nil {
slog.Warn("Error encoding product back to JSON for storage", "error", err)
encoded = []byte("error re-encoding product")
}
productInfos = append(productInfos, woolworthsProductInfoExtended{
ID: productID(strconv.Itoa(product.Stockcode)),
departmentID: departmentID(product.AdditionalAttributes.PiesProductDepartmentNodeID),
departmentDescription: product.AdditionalAttributes.Sapdepartmentname,
RawJSON: encoded,
Info: product,
Updated: time.Now(),
})
Expand Down

0 comments on commit 7384f33

Please sign in to comment.