worryg0d commented on code in PR #1943: URL: https://github.com/apache/cassandra-gocql-driver/pull/1943#discussion_r3187030061
########## example_interceptor_test.go: ########## @@ -0,0 +1,138 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * Content before git sha 34fdeebefcbf183ed7f916f931aa0586fdaa1b40 + * Copyright (c) 2016, The Gocql authors, + * provided under the BSD-3-Clause License. + * See the NOTICE file distributed with this work for additional information. + */ + +package gocql_test + +import ( + "context" + "log" + "time" + + gocql "github.com/apache/cassandra-gocql-driver/v2" +) + +type MyQueryAttemptInterceptor struct { + injectFault bool +} + +func (q MyQueryAttemptInterceptor) Intercept( + ctx context.Context, + attempt gocql.QueryAttempt, + handler gocql.QueryAttemptHandler, +) (*gocql.Iter, error) { + switch q := attempt.Statement.Statement().(type) { + case *gocql.Query: + // Inspect query + log.Println(q.Statement()) + case *gocql.Batch: + // Inspect batch + + log.Println(q.Entries[0].Stmt) + } Review Comment: I've just written a basic test to confirm that requests are immutable, and I'm able to mutate them in an interceptor: ```go type simpleInterceptor struct { logger StructuredLogger } func (s *simpleInterceptor) Intercept(ctx context.Context, attempt QueryAttempt, handler QueryAttemptHandler) (*Iter, error) { switch attempt.Statement.Statement().(type) { case *Query: query := attempt.Statement.Statement().(*Query) s.logger.Info("Intercepting query", NewLogFieldString("statement", query.Statement())) query.Consistency(One) default: } return handler(ctx) } func Test_QueryObjectImmutabilityInInterceptor(t *testing.T) { interceptor := &simpleInterceptor{logger: NewLogger(LogLevelInfo)} session := createSession(t, func(config *ClusterConfig) { config.ExecAttemptInterceptor = interceptor }) defer session.Close() expectedConsistency := Quorum query := session.Query("SELECT host_id FROM system.local").Consistency(expectedConsistency) var hostID string err := query.Scan(&hostID) if err != nil { t.Fatalf("Failed to scan host ID: %v", err) } require.Equal(t, expectedConsistency.String(), query.GetConsistency().String(), "Query object should not be mutated by the interceptor") } ``` Output: ``` 2026/05/05 11:28:35 logger.go:165: INF gocql: Intercepting query statement=DROP KEYSPACE IF EXISTS gocql_test 2026/05/05 11:28:37 logger.go:165: INF gocql: Intercepting query statement=CREATE KEYSPACE gocql_test WITH replication = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 } 2026/05/05 11:28:39 logger.go:165: INF gocql: Intercepting query statement=SELECT host_id FROM system.local --- FAIL: Test_QueryObjectImmutabilityInInterceptor (3.73s) /home/worry/projects/go/github.com/worryg0d/gocql/integration_test.go:1013: Error Trace: /home/worry/projects/go/github.com/worryg0d/gocql/integration_test.go:1013 Error: Not equal: expected: "QUORUM" actual : "ONE" Diff: --- Expected +++ Actual @@ -1 +1 @@ -QUORUM +ONE Test: Test_QueryObjectImmutabilityInInterceptor Messages: Query object should not be mutated by the interceptor FAIL FAIL github.com/apache/cassandra-gocql-driver/v2 3.736s ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

