Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(sdk-go): make CatalogProduct.Price nil for an unpriced product

Yudhi Armyndharise154fb715da6
diff --git a/sdk/go/catalog_product_decode_test.go b/sdk/go/catalog_product_decode_test.go
new file mode 100644
index 000000000..645adde1b
--- /dev/null
+++ b/sdk/go/catalog_product_decode_test.go
@@ -0,0 +1,24 @@
+package openwa
+
+import (
+ "encoding/json"
+ "testing"
+)
+
+// The gateway omits price for a catalog item that has none, and still sends a genuine 0. The two
+// must decode differently, or an unpriced item reads as free.
+func TestCatalogProductPriceTellsAbsentFromZero(t *testing.T) {
+ var unpriced, free CatalogProduct
+ if err := json.Unmarshal([]byte(`{"id":"p1","name":"A","url":"u","isAvailable":true}`), &unpriced); err != nil {
+ t.Fatalf("unmarshal unpriced: %v", err)
+ }
+ if err := json.Unmarshal([]byte(`{"id":"p2","name":"B","url":"u","isAvailable":true,"price":0,"currency":"IDR","priceFormatted":"IDR 0.00"}`), &free); err != nil {
+ t.Fatalf("unmarshal free: %v", err)
+ }
+ if unpriced.Price != nil {
+ t.Errorf("unpriced.Price = %v, want nil", *unpriced.Price)
+ }
+ if free.Price == nil || *free.Price != 0 {
+ t.Errorf("free.Price = %v, want a pointer to 0", free.Price)
+ }
+}
diff --git a/sdk/go/types_catalog.go b/sdk/go/types_catalog.go
index 7b85b1d17..ab8801005 100644
--- a/sdk/go/types_catalog.go
+++ b/sdk/go/types_catalog.go
@@ -26,16 +26,18 @@ func (q *CatalogProductsQuery) values() url.Values {
// CatalogProduct is a catalog product.
type CatalogProduct struct {
- ID string `json:"id"`
- Name string `json:"name"`
- Description *string `json:"description,omitempty"`
- Price float64 `json:"price,omitempty"`
- Currency string `json:"currency,omitempty"`
- PriceFormatted string `json:"priceFormatted,omitempty"`
- ImageURL *string `json:"imageUrl,omitempty"`
- URL string `json:"url,omitempty"`
- IsAvailable bool `json:"isAvailable,omitempty"`
- RetailerID string `json:"retailerId,omitempty"`
+ ID string `json:"id"`
+ Name string `json:"name"`
+ Description *string `json:"description,omitempty"`
+ // Price is nil when the product has no price (PriceFormatted is empty then too). A genuine
+ // price of 0 is a pointer to 0.
+ Price *float64 `json:"price,omitempty"`
+ Currency string `json:"currency,omitempty"`
+ PriceFormatted string `json:"priceFormatted,omitempty"`
+ ImageURL *string `json:"imageUrl,omitempty"`
+ URL string `json:"url,omitempty"`
+ IsAvailable bool `json:"isAvailable,omitempty"`
+ RetailerID string `json:"retailerId,omitempty"`
}
// ProductPagination is the pagination block for catalog products.