Skip to content

Conversation

@xernobyl
Copy link
Contributor

Summary

Converts the field_type column from VARCHAR to a PostgreSQL ENUM type, and implements a type-safe string-based enum in Go to ensure only valid field types can be used.

Changes

Database Schema

  • sql/001_initial_schema.sql: Updated to use field_type_enum instead of VARCHAR

Go Models (internal/models/feedback_records.go)

  • Added FieldType string-based enum type with constants
  • Implemented ValidFieldTypes map using map[FieldType]struct{} for efficient set membership checks
  • Added IsValid() and ParseFieldType() methods
  • Added UnmarshalJSON() for JSON validation
  • Updated structs to use FieldType instead of string:
    • FeedbackRecord.FieldType
    • CreateFeedbackRecordRequest.FieldType
    • ListFeedbackRecordsFilters.FieldType

Validation (internal/api/validation/validation.go)

  • Updated validateFieldType to use models.ParseFieldType() and ValidFieldTypes map
  • Added form decoder support for *FieldType in query parameters

OpenAPI Specification

  • Added enum values to field_type in response schema
  • Added enum values to field_type query parameter
  • Request schema already had correct enum values

Benefits

  • Type Safety: Compile-time and runtime validation ensures only valid field types
  • Data Integrity: PostgreSQL ENUM enforces constraints at database level
  • Better Documentation: OpenAPI spec clearly shows valid values
  • Performance: Efficient set membership checks using struct{} map values

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.

2 participants