GoPCA Shared Packages
This directory contains shared backend packages used by both GoPCA and GoCSV applications. These packages implement common functionality to ensure consistency and reduce code duplication.
Note: For shared frontend components (React/TypeScript), see packages/ui-components/
Package Structure
pkg/types/
Core types and data structures shared across applications.
json.go: Provides JSONFloat64 type for safe JSON marshaling of special float values (NaN, Inf)
- Marshals NaN and Inf as
null for JavaScript compatibility
- Unmarshals
null as NaN
- Includes helper methods:
Float64(), IsNaN(), IsInf()
pkg/utils/
Common utility functions for data processing.
Usage Examples
JSON-Safe Float Handling
import "github.com/bitjungle/gopca/pkg/types"
// Create a JSON-safe float
value := types.JSONFloat64(math.NaN())
// Marshal to JSON (becomes "null")
data, _ := json.Marshal(value)
// Check if value is NaN
if value.IsNaN() {
// Handle NaN case
}
Missing Value Detection
import "github.com/bitjungle/gopca/pkg/utils"
// Use default missing indicators
missingIndicators := utils.DefaultMissingValues()
// Check if a value is missing
if utils.IsMissingValue("NA", missingIndicators) {
// Handle missing value
}
// Count missing values in data
count := utils.CountMissingValues(dataSlice, missingIndicators)
Numeric Parsing
import "github.com/bitjungle/gopca/pkg/utils"
// Parse with decimal separator
value, err := utils.ParseNumericValue("123,45", ',')
// Parse with missing value detection
val, isMissing, err := utils.ParseNumericValueWithMissing(
"NA", '.', utils.DefaultMissingValues())
if isMissing {
// Value is missing (val = NaN)
}
Design Principles
- Consistency: Both applications handle data the same way
- Robustness: Comprehensive error handling and edge case coverage
- Performance: Efficient implementations with benchmarks
- Testability: High test coverage (>90%) with table-driven tests
- Simplicity: Clear, single-purpose functions following KISS principle
Testing
Run tests for shared packages:
go test ./pkg/...
Run with coverage:
go test -cover ./pkg/...
Contributing
When modifying shared packages:
- Ensure changes don't break existing functionality
- Add comprehensive tests for new features
- Update documentation as needed
- Test both GoPCA and GoCSV after changes
- Follow the project's coding standards from CLAUDE.md