Skip to content

Conversation

@yarcat
Copy link
Contributor

@yarcat yarcat commented Jul 8, 2025

Replace any-based values with typed Value system

This PR introduces a comprehensive type safety overhaul to the pennybase system, replacing the loosely-typed map[string]any approach with a structured Value type system.

Key Changes

🔧 Type System Overhaul

  • Replace Resource map[string]any with Resource map[string]Value
  • Introduce structured Value type with Number, Text, and List fields
  • Eliminate runtime type assertions with compile-time type safety

🏷️ Field Type Improvements

  • Convert FieldType from string constants to byte enum
  • Add proper String() method with efficient string table lookup
  • Provide FieldTypes map for string-to-enum conversion

🏗️ Server Architecture

  • Embed http.ServeMux directly in Server struct
  • Improve middleware patterns with cleaner auth handler
  • Modernize routing with typed path parameters

⚡ Concurrency Improvements

  • Replace mutex-based Broker with sync.Map for better performance
  • Eliminate read/write lock contention in event publishing
  • Optimize channel management for real-time events

🔄 Modern Go Features

  • Adopt Go 1.23's iter.Seq2 for database iteration
  • Use range-over-functions for cleaner iteration patterns
  • Leverage improved error handling with structured types

🧪 Test Suite Updates

  • Update all tests to use typed value constructors (textValue, numberValue, listValue)
  • Fix template syntax for new typed fields
  • Ensure backward compatibility at HTTP API level

Benefits

  • Type Safety: Compile-time validation of field types and values
  • Performance: Reduced type assertions and improved memory layout
  • Modern Go: Leverages Go 1.23 features like range-over-functions

Breaking Changes

  • API consumers must use typed Value objects instead of raw Go types
  • Template syntax changes from {{.field}} to {{.field.Text}} / {{.field.Number}}
  • Direct field access now requires type-specific property access

- Change Resource from map[string]any to map[string]Value with structured types
- Convert FieldType from string to byte enum with proper String() method
- Add comprehensive authentication with session cookies and basic auth
- Embed http.ServeMux directly in Server and improve context handling
- Replace mutex-based Broker with sync.Map for better concurrency
- Update all tests to use new typed value constructors
- Modernize DB iteration to use
@yarcat
Copy link
Contributor Author

yarcat commented Jul 8, 2025

That's a very experimental change. I just wanted to see whether it would provide you with any performance/memory benefits. Yeah, it kindaaa consumes slightly less memory based on the benchmarks, and sometimes behaves kinda faster, but overall it isn't much.

So yeah, feel free to reject, or maybe you would want to adopt only some of the changes. Not sure it's really worth it, as it breaks some of your previous assumptions.

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.

1 participant