Types Declares.
type Tp_basket struct {
InvoiceNumber string `json:"invoiceNumber,omitempty"`
SaleDateTime string `json:"saleDateTime,omitempty"`
SaleTimestamp string `json:"saleTimestamp,omitempty"`
Store TStoreStruct `json:"store,omitempty"`
Clerk TPClerkStruct `json:"clerk,omitempty"`
TerminalPoint string `json:"terminalPoint,omitempty"`
BasketItems []Tp_BasketItem `json:"basketItems,omitempty"`
Nett float64 `json:"nett,omitempty"`
VAT float64 `json:"vat,omitempty"`
Total float64 `json:"total,omitempty"`
}
type Tp_payment struct {
InvoiceNumber string `json:"invoiceNumber,omitempty"`
PayDateTime string `json:"payDateTime,omitempty"`
PayTimestamp string `json:"payTimestamp,omitempty"`
Paid float64 `json:"paid,omitempty"`
FinTransactionID string `json:"finTransactionId,omitempty"`
}
type TPClerkStruct struct {
Id string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
}
type TStoreStruct struct {
Id string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
}
main Kafka bits declare…
func main() {
var p *kafka.Producer
var client schemaregistry.Client
var serdes *jsonschema.SerializerConfig
var serializer *jsonschema.Serializer
// Create a new Schema Registry client
client, err = schemaregistry.NewClient(schemaregistry.NewConfig(vKafka.SchemaRegistryURL))
serdes = jsonschema.NewSerializerConfig()
serdes.AutoRegisterSchemas = false
serdes.EnableValidation = true
serdes.UseLatestVersion = true
serializer, err = jsonschema.NewSerializer(client, serde.ValueSerde, serdes)
t_SalesBasket, eventTimestamp, storeName, err := constructFakeBasket()
valueBytes, err := serializer.Serialize(vKafka.BasketTopicname, &t_SalesBasket)
kafkaMsg := kafka.Message{
TopicPartition: kafka.TopicPartition{
Topic: &vKafka.BasketTopicname,
Partition: kafka.PartitionAny,
},
Value: valueBytes, // This is the payload/body thats being posted
Key: []byte(storeName), // We us this to group the same transactions together in order, IE submitting/Debtor Bank.
Headers: []kafka.Header{{Key: "myBasketTopicnameHeader", Value: []byte("header values are binary")}},
}
}
Basic steps packaging of constructFakeBasket() Skipping some (lots ) bits of course…
func constructFakeBasket() (t_Basket types.Tp_basket eventTimestamp time.Time, storeName string, err error) {
t_Basket = types.Tp_basket{
InvoiceNumber: txnId,
SaleDateTime: eventTime,
SaleTimestamp: fmt.Sprint(eventTimestamp.UnixMilli()),
Store: store,
Clerk: clerk,
TerminalPoint: strconv.Itoa(terminalPoint),
BasketItems: arBasketItems,
Nett: nett_amount,
VAT: vat_amount,
Total: total_amount,
}
return t_Basket
}