Types

/doc/module/types.md (4.5 KB)

  1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# Types

The **`types`** module provides a comprehensive type system for [Project Forge](https://projectforge.dev) applications. It offers a collection of structured data types with built-in validation, serialization, and type safety features.

## Overview

This module provides a unified type system for representing and working with common data types in Go applications. It focuses on:

- **Type Safety**: Strongly typed representations with compile-time guarantees
- **JSON Serialization**: Built-in support for JSON marshaling and unmarshaling
- **Validation**: Configurable constraints and validation rules
- **Extensibility**: Composable type system with interfaces for custom types

## Key Features

### Comprehensive Type Coverage
- **Primitive Types**: String, Int, Float, Bool, Byte, Char
- **Complex Types**: List, Map, Set, OrderedMap, ValueMap
- **Temporal Types**: Date, Time, Timestamp, TimestampZoned
- **Special Types**: UUID, JSON, XML, Enum, Reference, Option
- **Utility Types**: Nil, Any, Unknown, Wrapped, Error

### Advanced Features
- **Type Conversion**: Safe conversion between compatible types
- **Default Values**: Configurable default value generation
- **Sorting Support**: Built-in sortability indicators
- **Scalar Detection**: Distinction between scalar and composite types
- **Range Constraints**: Min/max validation for numeric and string types

### JSON Integration
- **Custom Marshaling**: Optimized JSON serialization for all types
- **Flexible Parsing**: Intelligent type detection from JSON values
- **Wrapped Types**: Support for complex nested type structures

## Package Structure

### Core Types

- **`type.go`** - Core type interface and utilities
- `Type` interface definition
- Type casting and conversion utilities
- Common type checking functions

- **`string.go`** - String type with length and pattern validation
- **`int.go`** - Integer type with range constraints
- **`float.go`** - Floating-point type with precision controls
- **`bool.go`** - Boolean type representation

### Collection Types

- **`list.go`** - Ordered collections with type constraints
- **`map.go`** - Key-value mappings with typed keys and values
- **`set.go`** - Unique value collections
- **`orderedmap.go`** - Ordered key-value mappings
- **`valuemap.go`** - Specialized value mappings

### Temporal Types

- **`date.go`** - Date-only representations
- **`time.go`** - Time-only representations
- **`timestamp.go`** - Combined date and time
- **`timestampzoned.go`** - Timezone-aware timestamps

### Specialized Types

- **`uuid.go`** - UUID type with validation
- **`json.go`** - Raw JSON value handling
- **`xml.go`** - XML document representation
- **`enum.go`** - Enumeration type definitions
- **`option.go`** - Optional value handling

### Utility Types

- **`any.go`** - Dynamic type representation
- **`nil.go`** - Null value handling
- **`unknown.go`** - Unknown type representation
- **`wrapped.go`** - Type composition and nesting
- **`reference.go`** - Reference type for complex relationships

## Usage Examples

### Basic Type Usage

```go
// String type with constraints
stringType := &types.String{
MinLength: 1,
MaxLength: 100,
Pattern: "^[a-zA-Z0-9]+$",
}

// Integer type with range
intType := &types.Int{
Min: 0,
Max: 1000,
}

// Convert and validate values
value := stringType.From("hello")
if stringType.Sortable() {
// Handle sortable type
}
```

### Collection Types

```go
// List of strings
listType := &types.List{
Val: &types.String{MaxLength: 50},
}

// Map with string keys and integer values
mapType := &types.Map{
K: &types.String{},
V: &types.Int{Min: 0},
}
```

### Type Checking

```go
if types.IsString(someType) {
// Handle string type
}

if types.IsList(someType) {
listType := types.TypeAs[*types.List](someType)
// Work with list type
}
```

## Configuration

The types module supports various configuration options through individual type properties:

- **String constraints**: MinLength, MaxLength, Pattern
- **Numeric ranges**: Min, Max values for Int and Float
- **Collection settings**: Value types for List, key/value types for Map
- **Temporal formats**: Custom formatting for date/time types

## Source Code

- **Repository**: https://github.com/kyleu/projectforge/tree/main/module/types
- **License**: [CC0](https://creativecommons.org/publicdomain/zero/1.0) (Public Domain)
- **Author**: Kyle U (kyle@kyleu.com)

## See Also

- [Project Forge Documentation](https://projectforge.dev) - Complete documentation