diff --git a/.golangci.yaml b/.golangci.yaml index ab8dc72..a711e6e 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -98,5 +98,6 @@ issues: - goconst - init - gochecknoinits + - dupl run: timeout: 5m \ No newline at end of file diff --git a/go.mod b/go.mod index 049068f..232131b 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/zaphiro-technologies/protobuf -go 1.21 +go 1.23 require ( github.com/google/uuid v1.6.0 @@ -9,6 +9,7 @@ require ( ) require ( + github.com/ccoveille/go-safecast v1.2.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/google/go-cmp v0.5.8 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect diff --git a/go.sum b/go.sum index e890d4a..40103de 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/ccoveille/go-safecast v1.2.0 h1:H4X7aosepsU1Mfk+098CTdKpsDH0cfYJ2RmwXFjgvfc= +github.com/ccoveille/go-safecast v1.2.0/go.mod h1:QqwNjxQ7DAqY0C721OIO9InMk9zCwcsO7tnRuHytad8= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= diff --git a/go/grid/v1/data_test.go b/go/grid/v1/data_test.go index 3d226bf..c31ac2c 100644 --- a/go/grid/v1/data_test.go +++ b/go/grid/v1/data_test.go @@ -19,12 +19,13 @@ import ( "testing" "time" + "github.com/ccoveille/go-safecast" "github.com/google/uuid" "github.com/stretchr/testify/assert" "google.golang.org/protobuf/proto" ) -func generateData(dataType int, value uint64, timestamp int64) *Data { +func generateData(dataType int32, value uint64, timestamp int64) *Data { return &Data{ DataType: DataType(dataType), Value: &value, @@ -37,7 +38,7 @@ func generateDataSet(producerId string, data map[string]*Data) *DataSet { } func TestData(t *testing.T) { - for k := 0; k < 46; k++ { + for k := int32(0); k < 46; k++ { test := generateData(k, rand.Uint64(), time.Now().UnixNano()) buf, err := proto.Marshal(test) assert.NoError(t, err) @@ -52,7 +53,7 @@ func TestData(t *testing.T) { func TestDataSet(t *testing.T) { dataMap := map[string]*Data{} - for k := 0; k < 46; k++ { + for k := int32(0); k < 46; k++ { dataMap[uuid.New().String()] = generateData(k, rand.Uint64(), time.Now().UnixNano()) } test := generateDataSet("myDataSet", dataMap) @@ -66,7 +67,11 @@ func TestDataSet(t *testing.T) { } func BenchmarkDataSerialization(b *testing.B) { - test := generateData(rand.Intn(46), rand.Uint64(), time.Now().UnixNano()) + randDataType, err := safecast.ToInt32(rand.Intn(46)) + if err != nil { + b.Fatalf("Failed to convert int to int32: %v", err) + } + test := generateData(randDataType, rand.Uint64(), time.Now().UnixNano()) for i := 0; i < b.N; i++ { buf, _ := proto.Marshal(test) conf := &Data{} @@ -76,7 +81,7 @@ func BenchmarkDataSerialization(b *testing.B) { func BenchmarkDataSetSerialization(b *testing.B) { dataMap := map[string]*Data{} - for k := 0; k < 46; k++ { + for k := int32(0); k < 46; k++ { dataMap[uuid.New().String()] = generateData(k, rand.Uint64(), time.Now().UnixNano()) } test := generateDataSet("myDataSet", dataMap) diff --git a/go/grid/v1/event_test.go b/go/grid/v1/event_test.go index 0c7aa3b..a76d331 100644 --- a/go/grid/v1/event_test.go +++ b/go/grid/v1/event_test.go @@ -19,6 +19,7 @@ import ( "testing" "time" + "github.com/ccoveille/go-safecast" "github.com/google/uuid" "github.com/stretchr/testify/assert" "google.golang.org/protobuf/proto" @@ -27,7 +28,7 @@ import ( func generateEvent( eventId string, - eventSourceType int, + eventSourceType int32, eventSource string, occurredAt int64, message string, @@ -44,7 +45,7 @@ func generateEvent( } func TestEvent(t *testing.T) { - for k := 0; k < 4; k++ { + for k := int32(0); k < 4; k++ { test := generateEvent( uuid.NewString(), k, @@ -66,9 +67,13 @@ func TestEvent(t *testing.T) { } func BenchmarkEventSerialization(b *testing.B) { + randEventSourceType, err := safecast.ToInt32(rand.Intn(4)) + if err != nil { + b.Fatalf("Failed to convert int to int32: %v", err) + } test := generateEvent( uuid.NewString(), - rand.Intn(4), + randEventSourceType, uuid.NewString(), time.Now().UnixNano(), "my message benchmark event", @@ -99,7 +104,7 @@ func generateGridEvent( } func TestGridEvent(t *testing.T) { - for k := 0; k < 5; k++ { + for k := int32(0); k < 5; k++ { event := generateEvent( uuid.NewString(), k, @@ -127,9 +132,13 @@ func TestGridEvent(t *testing.T) { } func BenchmarkGridEventSerialization(b *testing.B) { + randEventSourceType, err := safecast.ToInt32(rand.Intn(4)) + if err != nil { + b.Fatalf("Failed to convert int to int32: %v", err) + } event := generateEvent( uuid.NewString(), - rand.Intn(4), + randEventSourceType, uuid.NewString(), time.Now().UnixNano(), "my message benchmark grid event", diff --git a/go/grid/v1/fault_test.go b/go/grid/v1/fault_test.go index 5b2d33b..49d3845 100644 --- a/go/grid/v1/fault_test.go +++ b/go/grid/v1/fault_test.go @@ -19,6 +19,7 @@ import ( "testing" "time" + "github.com/ccoveille/go-safecast" "github.com/google/uuid" "github.com/stretchr/testify/assert" "google.golang.org/protobuf/proto" @@ -27,7 +28,7 @@ import ( func generateFault( faultId string, - faultKind, phaseCode int, + faultKind, phaseCode int32, updatedAt int64, faultyEquipmentId string, ) *Fault { @@ -41,11 +42,15 @@ func generateFault( } func TestFault(t *testing.T) { - for k := 0; k < 5; k++ { + for k := int32(0); k < 5; k++ { + randPhaseCode, err := safecast.ToInt32(rand.Intn(26)) + if err != nil { + t.Fatalf("Failed to convert int to int32: %v", err) + } test := generateFault( uuid.NewString(), k, - rand.Intn(26), + randPhaseCode, time.Now().UnixNano(), uuid.NewString(), ) @@ -62,10 +67,18 @@ func TestFault(t *testing.T) { } func BenchmarkFaultSerialization(b *testing.B) { + randFaultKind, err := safecast.ToInt32(rand.Intn(4)) + if err != nil { + b.Fatalf("Failed to convert int to int32: %v", err) + } + randPhaseCode, err := safecast.ToInt32(rand.Intn(26)) + if err != nil { + b.Fatalf("Failed to convert int to int32: %v", err) + } test := generateFault( uuid.NewString(), - rand.Intn(4), - rand.Intn(26), + randFaultKind, + randPhaseCode, time.Now().UnixNano(), uuid.NewString(), ) @@ -87,8 +100,12 @@ func generateLineFault( } func TestLineFault(t *testing.T) { - for k := 0; k < 5; k++ { - fault := generateFault(uuid.NewString(), k, rand.Intn(26), time.Now().UnixNano(), "line1") + for k := int32(0); k < 5; k++ { + randPhaseCode, err := safecast.ToInt32(rand.Intn(26)) + if err != nil { + t.Fatalf("Failed to convert int to int32: %v", err) + } + fault := generateFault(uuid.NewString(), k, randPhaseCode, time.Now().UnixNano(), "line1") test := generateLineFault(fault, rand.Float32()) buf, err := proto.Marshal(test) assert.NoError(t, err) @@ -100,10 +117,18 @@ func TestLineFault(t *testing.T) { } func BenchmarkLineFaultSerialization(b *testing.B) { + randFaultKind, err := safecast.ToInt32(rand.Intn(4)) + if err != nil { + b.Fatalf("Failed to convert int to int32: %v", err) + } + randPhaseCode, err := safecast.ToInt32(rand.Intn(26)) + if err != nil { + b.Fatalf("Failed to convert int to int32: %v", err) + } fault := generateFault( uuid.NewString(), - rand.Intn(4), - rand.Intn(26), + randFaultKind, + randPhaseCode, time.Now().UnixNano(), "line1", ) @@ -126,11 +151,15 @@ func generateEquipmentFault( } func TestEquipmentFault(t *testing.T) { - for k := 0; k < 5; k++ { + for k := int32(0); k < 5; k++ { + randPhaseCode, err := safecast.ToInt32(rand.Intn(26)) + if err != nil { + t.Fatalf("Failed to convert int to int32: %v", err) + } fault := generateFault( uuid.NewString(), k, - rand.Intn(26), + randPhaseCode, time.Now().UnixNano(), "equipment1", ) @@ -145,10 +174,18 @@ func TestEquipmentFault(t *testing.T) { } func BenchmarkEquipmentFaultSerialization(b *testing.B) { + randFaultKind, err := safecast.ToInt32(rand.Intn(4)) + if err != nil { + b.Fatalf("Failed to convert int to int32: %v", err) + } + randPhaseCode, err := safecast.ToInt32(rand.Intn(26)) + if err != nil { + b.Fatalf("Failed to convert int to int32: %v", err) + } fault := generateFault( uuid.NewString(), - rand.Intn(4), - rand.Intn(26), + randFaultKind, + randPhaseCode, time.Now().UnixNano(), "equipment1", ) diff --git a/go/platform/v1/task_test.go b/go/platform/v1/task_test.go index 7dcc06b..1dd7b7f 100644 --- a/go/platform/v1/task_test.go +++ b/go/platform/v1/task_test.go @@ -19,6 +19,7 @@ import ( "testing" "time" + "github.com/ccoveille/go-safecast" "github.com/stretchr/testify/assert" "google.golang.org/protobuf/proto" protoreflect "google.golang.org/protobuf/reflect/protoreflect" @@ -32,7 +33,7 @@ func generateTask(taskType TaskType, createdAt int64) *Task { } func TestTask(t *testing.T) { - for k := 0; k < 5; k++ { + for k := int32(0); k < 5; k++ { test := generateTask(TaskType(k), time.Now().UnixNano()) buf, err := proto.Marshal(test) assert.NoError(t, err) @@ -58,7 +59,7 @@ func generateNotification( } func TestNotification(t *testing.T) { - for k := 0; k < 5; k++ { + for k := int32(0); k < 5; k++ { test := generateNotification( NotificationType(k), time.Now().UnixNano(), @@ -94,8 +95,12 @@ func TestTriggerNotification(t *testing.T) { } func BenchmarkNotificationSerialization(b *testing.B) { + randInt32, err := safecast.ToInt32(rand.Intn(4)) + if err != nil { + b.Fatalf("Failed to convert int to int32: %v", err) + } test := generateNotification( - NotificationType(rand.Intn(4)), + NotificationType(randInt32), time.Now().UnixNano(), "1", ) @@ -107,7 +112,11 @@ func BenchmarkNotificationSerialization(b *testing.B) { } func BenchmarkTaskSerialization(b *testing.B) { - test := generateTask(TaskType(rand.Intn(5)), time.Now().UnixNano()) + randInt32, err := safecast.ToInt32(rand.Intn(5)) + if err != nil { + b.Fatalf("Failed to convert int to int32: %v", err) + } + test := generateTask(TaskType(randInt32), time.Now().UnixNano()) for i := 0; i < b.N; i++ { buf, _ := proto.Marshal(test) conf := &Task{}