github.com/geniusesgroup/libgo@v0.0.0-20220713101832-828057a9d3d4/authorization/product-sale.go (about) 1 /* For license and copyright information please see LEGAL file in repository */ 2 3 package authorization 4 5 import ( 6 etime "../earth-time" 7 er "../error" 8 "../json" 9 "../syllab" 10 ) 11 12 // Product store needed data to authorize product as auction or any other order type 13 type Product struct { 14 AllowUserID [32]byte `index-hash:"ID" json:",string"` // If 0 means this sale is not just for specific UserID! can be any OrgID e.g. DistributionCenterID 15 AllowUserType UserType 16 AllowWeekdays etime.Weekdays 17 AllowDayhours etime.Dayhours 18 GroupID [32]byte `index-hash:"ID" json:",string"` // it can be 0 and means sale is global! 19 MinNumBuy uint64 // Minimum number to buy in this auction to use for sale-off(Discount) 20 StockNumber uint64 // 0 for unlimited until related product exist to sell! 21 LiveUntil etime.Time 22 } 23 24 // SyllabDecoder decode syllab to given Product 25 func (p *Product) SyllabDecoder(buf []byte, stackIndex uint32) (err *er.Error) { 26 // var add, ln uint32 27 // var tempSlice []byte 28 29 if uint32(len(buf)) < p.SyllabStackLen() { 30 err = syllab.ErrSyllabDecodeSmallSlice 31 return 32 } 33 34 copy(p.AllowUserID[:], buf[0:]) 35 p.AllowUserType = UserType(syllab.GetUInt8(buf, 32)) 36 p.AllowWeekdays = etime.Weekdays(syllab.GetUInt8(buf, 33)) 37 p.AllowDayhours = etime.Dayhours(syllab.GetUInt32(buf, 34)) 38 copy(p.GroupID[:], buf[38:]) 39 p.MinNumBuy = syllab.GetUInt64(buf, 70) 40 p.StockNumber = syllab.GetUInt64(buf, 78) 41 p.LiveUntil = etime.Time(syllab.GetInt64(buf, 86)) 42 return 43 } 44 45 // SyllabEncoder encode given Product to syllab format 46 func (p *Product) SyllabEncoder(buf []byte, stackIndex, heapIndex uint32) (nextHeapAddr uint32) { 47 copy(buf[0:], p.AllowUserID[:]) 48 syllab.SetUInt8(buf, 32, uint8(p.AllowUserType)) 49 syllab.SetUInt8(buf, 33, uint8(p.AllowWeekdays)) 50 syllab.SetUInt32(buf, 34, uint32(p.AllowDayhours)) 51 copy(buf[38:], p.GroupID[:]) 52 syllab.SetUInt64(buf, 70, p.MinNumBuy) 53 syllab.SetUInt64(buf, 78, p.StockNumber) 54 syllab.SetInt64(buf, 86, int64(p.LiveUntil)) 55 return 56 } 57 58 // SyllabStackLen return stack length of Product 59 func (p *Product) SyllabStackLen() (ln uint32) { 60 return 94 61 } 62 63 // SyllabHeapLen return heap length of Product 64 func (p *Product) SyllabHeapLen() (ln uint32) { 65 return 66 } 67 68 // SyllabLen return whole length of Product 69 func (p *Product) SyllabLen() (ln int) { 70 return int(p.SyllabStackLen() + p.SyllabHeapLen()) 71 } 72 73 // JSONDecoder decode json to given Product 74 func (p *Product) JSONDecoder(decoder json.DecoderUnsafeMinifed) (err *er.Error) { 75 for err == nil { 76 var keyName = decoder.DecodeKey() 77 switch keyName { 78 case "AllowUserID": 79 err = decoder.DecodeByteArrayAsBase64(p.AllowUserID[:]) 80 case "AllowUserType": 81 var num uint8 82 num, err = decoder.DecodeUInt8() 83 p.AllowUserType = UserType(num) 84 case "AllowWeekdays": 85 var num uint8 86 num, err = decoder.DecodeUInt8() 87 p.AllowWeekdays = etime.Weekdays(num) 88 case "AllowDayhours": 89 var num uint32 90 num, err = decoder.DecodeUInt32() 91 p.AllowDayhours = etime.Dayhours(num) 92 case "GroupID": 93 err = decoder.DecodeByteArrayAsBase64(p.GroupID[:]) 94 case "MinNumBuy": 95 p.MinNumBuy, err = decoder.DecodeUInt64() 96 case "StockNumber": 97 p.StockNumber, err = decoder.DecodeUInt64() 98 case "LiveUntil": 99 var num int64 100 num, err = decoder.DecodeInt64() 101 p.LiveUntil = etime.Time(num) 102 default: 103 err = decoder.NotFoundKeyStrict() 104 } 105 106 if len(decoder.Buf) < 3 { 107 return 108 } 109 } 110 return 111 } 112 113 // JSONEncoder encode given Product to json format. 114 func (p *Product) JSONEncoder(encoder json.Encoder) { 115 encoder.EncodeString(`{"AllowUserID":"`) 116 encoder.EncodeByteSliceAsBase64(p.AllowUserID[:]) 117 118 encoder.EncodeString(`","AllowUserType":`) 119 encoder.EncodeUInt8(uint8(p.AllowUserType)) 120 121 encoder.EncodeString(`,"AllowWeekdays":`) 122 encoder.EncodeUInt8(uint8(p.AllowWeekdays)) 123 124 encoder.EncodeString(`,"AllowDayhours":`) 125 encoder.EncodeUInt32(uint32(p.AllowDayhours)) 126 127 encoder.EncodeString(`,"GroupID":"`) 128 encoder.EncodeByteSliceAsBase64(p.GroupID[:]) 129 130 encoder.EncodeString(`","MinNumBuy":`) 131 encoder.EncodeUInt64(p.MinNumBuy) 132 133 encoder.EncodeString(`,"StockNumber":`) 134 encoder.EncodeUInt64(p.StockNumber) 135 136 encoder.EncodeString(`,"LiveUntil":`) 137 encoder.EncodeInt64(int64(p.LiveUntil)) 138 139 encoder.EncodeByte('}') 140 } 141 142 // JSONLen return json needed len to encode! 143 func (p *Product) JSONLen() (ln int) { 144 ln = 455 145 return 146 }