Skip to content

Conversation

Copy link

Copilot AI commented Jul 1, 2025

This PR implements a complete type-safe Go error code generator that produces compile-time validated error constants and factory functions from user-supplied JSON or YAML configuration files.

🎯 Key Features

  • Type-safe error constants - Generate compile-time validated error codes with zero runtime lookups
  • Factory functions - Simple, consistent error creation API using rescode.New
  • YAML/JSON input - Flexible configuration format with auto-detection
  • HTTP/gRPC integration - Built-in status code mapping
  • CLI tool (rescodegen) - Compatible with go:generate workflow
  • High performance - Up to 145x faster than runtime map-based approaches

🚀 Performance Improvements

Benchmarks show dramatic performance gains over traditional runtime map-based error handling:

BenchmarkGenerated_PolicyNotFound-2    1000000000    0.31 ns/op    0 B/op    0 allocs/op
BenchmarkLegacy_PolicyNotFound-2           26802039   45.16 ns/op   80 B/op    1 allocs/op

145x faster with zero allocations compared to runtime map lookups.

📋 Usage Example

Define errors in YAML:

- code: 1001
  key: UserNotFound
  message: User not found
  http: 404
  grpc: 5
  desc: The specified user could not be found

Generate type-safe Go code:

go run github.com/restayway/rescode/cmd/rescodegen --input errors.yaml --output errors_gen.go --package main

Use the generated errors:

err := UserNotFound()
fmt.Printf("Error: %v (HTTP: %d, gRPC: %d)", err, err.HttpCode, err.RpcCode)

🧪 Testing & Quality

  • 100% test coverage for core package
  • 91.2% test coverage for generator
  • Comprehensive CLI tests
  • Performance benchmarks comparing generated vs legacy approaches
  • Working examples demonstrating basic and microservice usage

📦 What's Included

  • Core rescode package with RC struct and error handling API
  • CLI tool rescodegen for code generation
  • Comprehensive test suite with benchmarks
  • Working examples (basic usage and microservice integration)
  • Complete documentation and README
  • MIT license

The solution eliminates runtime lookups entirely by generating compile-time constants and factory functions, providing both type safety and exceptional performance for Go error handling.

Fixes #1.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Co-authored-by: 9ssi7 <76786120+9ssi7@users.noreply.github.com>
Copilot AI changed the title [WIP] Type-Safe Go Error Code Generator from JSON/YAML Definitions Implement type-safe Go error code generator from JSON/YAML definitions Jul 1, 2025
Copilot AI requested a review from salihguru July 1, 2025 23:38
* Added `coverage.html` to `.gitignore` to exclude coverage reports from version control.
* Enhanced `README.md` with detailed performance benchmarks and comparisons of error handling approaches.
* Introduced new benchmarks for `StaticCode` and `VarCode` error handling methods in `benchmark_test.go`.
* Updated `Makefile` to include commands for running benchmarks and viewing coverage reports.
@salihguru salihguru marked this pull request as ready for review July 2, 2025 00:34
@salihguru salihguru merged commit 7047f30 into main Jul 2, 2025
@hmert hmert deleted the copilot/fix-1 branch December 7, 2025 09:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Type-Safe Go Error Code Generator from JSON/YAML Definitions

2 participants