Changed the assets to 2 bytes; implemented a Expire Datetime with Unix; Changed README.md

This commit is contained in:
Jan 2025-01-22 22:24:33 +01:00
parent 75d8f343ea
commit 0c09cc0db0
3 changed files with 21 additions and 10 deletions

View File

@ -8,13 +8,15 @@ An Package to interact with an mock stock exchange
| ----- | ------ |
| Key | 8 Bytes |
| Operation | Byte |
| Asset | Byte |
| Asset | 2 Bytes |
| Price | Float64 |
| Volume | Float64 |
| Expire (Unix) | uint64 |
| CRC | 2 Bytes |
## TODOS
- [ ] Test it
- [ ] Make it configureable
- [ ] Implement expire date
- [x] Implement expire date
- [ ] Change Key to uuid
- [ ] Document it

19
spf.go
View File

@ -12,20 +12,21 @@ import (
type SimpleFinancePackage struct {
Key [8]byte // 8 bytes for the key
Operation byte // 1 byte for operation
Asset byte // 1 byte for asset
Asset uint16 // 2 bytes for asset
Price float64 // 8 bytes for price (float64 for precision)
Volume float64 // 8 bytes for volume
Expire uint64 // 8 bytes for expiration date
CRC uint16 // 2 bytes for CRC
}
func ParseSimpleFinanacePackage(data []byte) (*SimpleFinancePackage, error) {
if len(data) != 28 {
return nil, errors.New("invalid packet size, expected 28 bytes")
if len(data) != 37 {
return nil, errors.New("invalid packet size, expected 37 bytes")
}
//Check CRC
check := binary.BigEndian.Uint16(data[26:])
if check != crc.Calculate(data[:26]) {
check := binary.BigEndian.Uint16(data[35:])
if check != crc.Calculate(data[:35]) {
return nil, errors.New("CRC check failed")
}
@ -41,9 +42,12 @@ func ParseSimpleFinanacePackage(data []byte) (*SimpleFinancePackage, error) {
if packet.Price < 0 || packet.Volume < 0 {
return nil, errors.New("price and volume must be non-negative")
}
if packet.Operation > 127 { // Example: Valid operation values are 0-127
if packet.Operation > 127 { // Valid operation values are 0-127
return nil, errors.New("invalid operation value")
}
if packet.Expire < 946684800 { // Expire date must be after 2000-01-01
return nil, errors.New("invalid expiration date")
}
return &packet, nil
}
@ -67,6 +71,9 @@ func EncodeSimpleFinanacePackage(packet SimpleFinancePackage) ([]byte, error) {
if err := binary.Write(buf, binary.BigEndian, packet.Volume); err != nil {
return nil, fmt.Errorf("failed to encode volume: %w", err)
}
if err := binary.Write(buf, binary.BigEndian, packet.Expire); err != nil {
return nil, fmt.Errorf("failed to encode expiration date: %w", err)
}
// Calculate and write CRC
packet.CRC = crc.Calculate(buf.Bytes())

View File

@ -15,6 +15,7 @@ func TestEncodeDecodeCustomPacket(t *testing.T) {
Asset: 2,
Price: 1234.56,
Volume: 7890.12,
Expire: 946684801,
}
// Encode the packet
@ -24,8 +25,8 @@ func TestEncodeDecodeCustomPacket(t *testing.T) {
}
// Check the encoded length
if len(encoded) != 28 {
t.Fatalf("Encoded data has incorrect length: got %d, want 28", len(encoded))
if len(encoded) != 37 {
t.Fatalf("Encoded data has incorrect length: got %d, want 37", len(encoded))
}
// Decode the packet
@ -76,6 +77,7 @@ func TestInvalidPacketValues(t *testing.T) {
Asset: 2,
Price: 1234.56,
Volume: 7890.12,
Expire: 946684801,
}
encoded, _ := EncodeSimpleFinanacePackage(validPacket)