@@ -505,17 +505,28 @@ func (k *kafkaWriter) WriteBatch(ctx context.Context, msg service.MessageBatch)
505
505
return service .ErrNotConnected
506
506
}
507
507
508
+ topicExecutor := msg .InterpolationExecutor (k .topic )
509
+ keyExecutor := msg .InterpolationExecutor (k .key )
510
+ var partitionExecutor * service.MessageBatchInterpolationExecutor
511
+ if k .partition != nil {
512
+ partitionExecutor = msg .InterpolationExecutor (k .partition )
513
+ }
514
+ var timestampExecutor * service.MessageBatchInterpolationExecutor
515
+ if k .timestamp != nil {
516
+ timestampExecutor = msg .InterpolationExecutor (k .timestamp )
517
+ }
518
+
508
519
boff := k .backoffCtor ()
509
520
510
521
userDefinedHeaders := k .buildUserDefinedHeaders (k .staticHeaders )
511
522
msgs := []* sarama.ProducerMessage {}
512
523
513
524
for i := 0 ; i < len (msg ); i ++ {
514
- key , err := msg . TryInterpolatedBytes ( i , k . key )
525
+ key , err := keyExecutor . TryBytes ( i )
515
526
if err != nil {
516
527
return fmt .Errorf ("key interpolation error: %w" , err )
517
528
}
518
- topic , err := msg . TryInterpolatedString ( i , k . topic )
529
+ topic , err := topicExecutor . TryString ( i )
519
530
if err != nil {
520
531
return fmt .Errorf ("topic interpolation error: %w" , err )
521
532
}
@@ -543,8 +554,8 @@ func (k *kafkaWriter) WriteBatch(ctx context.Context, msg service.MessageBatch)
543
554
// partitioner. Although samara will (currently) ignore the partition
544
555
// field when not using a manual partitioner, we should only set it when
545
556
// we explicitly want that.
546
- if k . partition != nil {
547
- partitionString , err := msg . TryInterpolatedString ( i , k . partition )
557
+ if partitionExecutor != nil {
558
+ partitionString , err := partitionExecutor . TryString ( i )
548
559
if err != nil {
549
560
return fmt .Errorf ("partition interpolation error: %w" , err )
550
561
}
@@ -563,8 +574,8 @@ func (k *kafkaWriter) WriteBatch(ctx context.Context, msg service.MessageBatch)
563
574
nextMsg .Partition = int32 (partitionInt )
564
575
}
565
576
566
- if k . timestamp != nil {
567
- if tsStr , err := msg . TryInterpolatedString ( i , k . timestamp ); err != nil {
577
+ if timestampExecutor != nil {
578
+ if tsStr , err := timestampExecutor . TryString ( i ); err != nil {
568
579
return fmt .Errorf ("timestamp interpolation error: %w" , err )
569
580
} else {
570
581
if ts , err := strconv .ParseInt (tsStr , 10 , 64 ); err != nil {
0 commit comments