@@ -138,14 +138,16 @@ func (*Listing) ProtoMessage() {}
138
138
139
139
// NewListingFromProtobuf - return Listing from pb.Listing
140
140
func NewListingFromProtobuf (l * pb.Listing ) (* Listing , error ) {
141
- if l .Metadata .Version == 0 {
142
- l .Metadata .Version = ListingVersion
141
+ clonedListing := proto .Clone (l ).(* pb.Listing )
142
+
143
+ if clonedListing .Metadata .Version == 0 {
144
+ clonedListing .Metadata .Version = ListingVersion
143
145
}
144
- if l .Metadata .EscrowTimeoutHours == 0 {
145
- l .Metadata .EscrowTimeoutHours = DefaultEscrowTimeout
146
+ if clonedListing .Metadata .EscrowTimeoutHours == 0 {
147
+ clonedListing .Metadata .EscrowTimeoutHours = DefaultEscrowTimeout
146
148
}
147
149
return & Listing {
148
- listingProto : l ,
150
+ listingProto : clonedListing ,
149
151
}, nil
150
152
}
151
153
@@ -376,6 +378,59 @@ func ExtractIDFromListing(data []byte) (*pb.ID, error) {
376
378
return vendorPlay , nil
377
379
}
378
380
381
+ // Normalize converts legacy schema listing data from other users on the network
382
+ // to fit the latest schema for consumption via the API for local use. NOTE: Legacy
383
+ // nodes do not understand the latest schema. As such, normalized listings must not
384
+ // be used as part of the RicardianContract and must be serialized and used as they
385
+ // were provided by the originating node.
386
+ func (l * Listing ) Normalize () (* Listing , error ) {
387
+ if l == nil {
388
+ return nil , errors .New ("nil listing cannot be normalized" )
389
+ }
390
+
391
+ if l .GetVersion () == ListingVersion {
392
+ return l , nil
393
+ }
394
+
395
+ nl , err := NewListingFromProtobuf (l .listingProto )
396
+ if err != nil {
397
+ return nil , fmt .Errorf ("creating listing clone: %s" , err .Error ())
398
+ }
399
+
400
+ nlp := nl .GetProtobuf ()
401
+ nlp .Metadata .Version = ListingVersion
402
+
403
+ if p , err := l .GetPrice (); err != nil {
404
+ return nil , fmt .Errorf ("get price: %s" , err .Error ())
405
+ } else {
406
+ nlp .Item .BigPrice = p .Amount .String ()
407
+ nlp .Item .PriceCurrency = & pb.CurrencyDefinition {
408
+ Code : p .Currency .Code .String (),
409
+ Divisibility : uint32 (p .Currency .Divisibility ),
410
+ }
411
+ }
412
+
413
+ if ss , err := l .GetSkus (); err != nil {
414
+ return nil , fmt .Errorf ("get skus: %s" , err .Error ())
415
+ } else {
416
+ nlp .Item .Skus = ss
417
+ }
418
+
419
+ if sos , err := l .GetShippingOptions (); err != nil {
420
+ return nil , fmt .Errorf ("get shipping options: %s" , err .Error ())
421
+ } else {
422
+ nlp .ShippingOptions = sos
423
+ }
424
+
425
+ if cs , err := l .GetCoupons (); err != nil {
426
+ return nil , fmt .Errorf ("get coupons: %s" , err .Error ())
427
+ } else {
428
+ nlp .Coupons = cs .GetProtobuf ()
429
+ }
430
+
431
+ return nl , nil
432
+ }
433
+
379
434
// GetProtobuf returns the current state of pb.Listing managed by Listing
380
435
func (l * Listing ) GetProtobuf () * pb.Listing {
381
436
return l .listingProto
@@ -695,18 +750,18 @@ func (l *Listing) GetImages() []*ListingImage {
695
750
696
751
// GetSkus returns the listing SKUs
697
752
func (l * Listing ) GetSkus () ([]* pb.Listing_Item_Sku , error ) {
753
+ var ss = make ([]* pb.Listing_Item_Sku , len (l .listingProto .Item .Skus ))
754
+ for i , s := range l .listingProto .Item .Skus {
755
+ ss [i ] = proto .Clone (s ).(* pb.Listing_Item_Sku )
756
+ }
698
757
switch l .GetVersion () {
699
758
case 3 , 4 :
700
- for _ , sku := range l .listingProto .Item .Skus {
701
- surcharge := new (big.Int ).SetInt64 (sku .Surcharge )
702
- quantity := new (big.Int ).SetInt64 (sku .Quantity )
703
- sku .BigSurcharge = surcharge .String ()
704
- sku .BigQuantity = quantity .String ()
705
- sku .Quantity = 0
706
- sku .Surcharge = 0
759
+ for _ , sku := range ss {
760
+ sku .BigSurcharge = big .NewInt (sku .Surcharge ).String ()
761
+ sku .BigQuantity = big .NewInt (sku .Quantity ).String ()
707
762
}
708
763
}
709
- return l . listingProto . Item . Skus , nil
764
+ return ss , nil
710
765
}
711
766
712
767
//GetLanguage return listing's language
@@ -858,6 +913,24 @@ func (l *Listing) GetCoupons() (ListingCoupons, error) {
858
913
// ListingCoupons is a set of listing coupons
859
914
type ListingCoupons []* ListingCoupon
860
915
916
+ // GetProtobuf converts ListingCoupons into its protobuf representation
917
+ func (cs ListingCoupons ) GetProtobuf () []* pb.Listing_Coupon {
918
+ var cspb = make ([]* pb.Listing_Coupon , len (cs ))
919
+ for i , c := range cs {
920
+ cspb [i ] = & pb.Listing_Coupon {
921
+ Title : c .GetTitle (),
922
+ PercentDiscount : c .GetPercentOff (),
923
+ BigPriceDiscount : c .GetAmountOff ().Amount .String (),
924
+ }
925
+ if hash , err := c .GetRedemptionHash (); err == nil {
926
+ cspb [i ].Code = & pb.Listing_Coupon_Hash {Hash : hash }
927
+ } else if code , err := c .GetRedemptionCode (); err == nil {
928
+ cspb [i ].Code = & pb.Listing_Coupon_DiscountCode {DiscountCode : code }
929
+ }
930
+ }
931
+ return cspb
932
+ }
933
+
861
934
// ListingCoupon represents an coupon which can be applied to a listing for a discount
862
935
type ListingCoupon struct {
863
936
listing * Listing
@@ -932,6 +1005,25 @@ func (c *ListingCoupon) updateProtoHash(hash string) error {
932
1005
return errors .New ("unable to update missing coupon proto" )
933
1006
}
934
1007
1008
+ // GetShippingOptions returns all shipping options
1009
+ func (l * Listing ) GetShippingOptions () ([]* pb.Listing_ShippingOption , error ) {
1010
+ var so = make ([]* pb.Listing_ShippingOption , len (l .listingProto .ShippingOptions ))
1011
+ for i , s := range l .listingProto .ShippingOptions {
1012
+ so [i ] = proto .Clone (s ).(* pb.Listing_ShippingOption )
1013
+ }
1014
+ switch l .GetVersion () {
1015
+ case 3 , 4 :
1016
+ for _ , o := range so {
1017
+ for _ , s := range o .Services {
1018
+ s .BigPrice = big .NewInt (int64 (s .Price )).String ()
1019
+ s .BigAdditionalItemPrice = big .NewInt (int64 (s .AdditionalItemPrice )).String ()
1020
+ }
1021
+ }
1022
+ }
1023
+ return so , nil
1024
+
1025
+ }
1026
+
935
1027
// GetShippingRegions returns all region strings for the defined shipping
936
1028
// services
937
1029
func (l * Listing ) GetShippingRegions () ([]string , []string ) {
0 commit comments