Skip to content

Commit 9aaeb45

Browse files
committed
Add New. Update Readme
1 parent 8c938c2 commit 9aaeb45

File tree

3 files changed

+314
-92
lines changed

3 files changed

+314
-92
lines changed

README.md

+29-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ func processPreferences(prefs Preferences) {
2121
thirdPartyLibrary.EnableFeatureX(prefs.UseFeatureX)
2222
thirdPartyLibrary.SetThreshold(prefs.Threshold)
2323
}
24+
25+
func main() {
26+
var prefs Preferences
27+
processPreferences(prefs)
28+
}
2429
```
2530

2631
There is no real way to know if `UseFeatureX` or `Threshold` have been set or not.
@@ -33,8 +38,6 @@ type Preferences struct {
3338
Threshold u.Int
3439
}
3540

36-
var prefs Preferences
37-
3841
func processPreferences(prefs Preferences) {
3942
// #winning
4043
if prefs.UseFeatureX.IsSet() {
@@ -44,6 +47,12 @@ func processPreferences(prefs Preferences) {
4447
thirdPartyLibrary.SetThreshold(prefs.Threshold.Get())
4548
}
4649
}
50+
51+
func main() {
52+
var prefs Preferences
53+
processPreferences(prefs)
54+
}
55+
4756
```
4857

4958
## Why should any of this matter?
@@ -53,6 +62,8 @@ Perhaps the default value for a preference is `true` and you only want to set it
5362

5463
## Usage
5564

65+
### Basic Usage
66+
5667
```go
5768
var myVar u.Int
5869

@@ -72,6 +83,22 @@ myVar.Unset()
7283
fmt.Println(myVar.IsSet()) // false
7384
```
7485

86+
### Structs
87+
88+
`New` methods are provided for setting values in structs.
89+
90+
```go
91+
type MyStruct struct {
92+
MyVar u.Int
93+
}
94+
95+
myStruct := MyStruct{
96+
MyVar: u.NewInt(42),
97+
}
98+
99+
100+
}
101+
75102
## Supported types
76103

77104
- `u.Bool`

u.go

+95
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,147 @@ package u
33
// Bool is a `bool` that can be unset
44
type Bool = Var[bool]
55

6+
// NewBool creates a new Bool with the given value
7+
func NewBool(val bool) Bool {
8+
return NewVar(val)
9+
}
10+
611
// String is a `string` that can be unset
712
type String = Var[string]
813

14+
// NewString creates a new String with the given value
15+
func NewString(val string) String {
16+
return NewVar(val)
17+
}
18+
919
// Float64 is a `float64` that can be unset
1020
type Float64 = Var[float64]
1121

22+
// NewFloat64 creates a new Float64 with the given value
23+
func NewFloat64(val float64) Float64 {
24+
return NewVar(val)
25+
}
26+
1227
// Float32 is a `float32` that can be unset
1328
type Float32 = Var[float32]
1429

30+
// NewFloat32 creates a new Float32 with the given value
31+
func NewFloat32(val float32) Float32 {
32+
return NewVar(val)
33+
}
34+
1535
// Uint is a `uint` that can be unset
1636
type Uint = Var[uint]
1737

38+
// NewUint creates a new Uint with the given value
39+
func NewUint(val uint) Uint {
40+
return NewVar(val)
41+
}
42+
1843
// Uint8 is a `uint8` that can be unset
1944
type Uint8 = Var[uint8]
2045

46+
// NewUint8 creates a new Uint8 with the given value
47+
func NewUint8(val uint8) Uint8 {
48+
return NewVar(val)
49+
}
50+
2151
// Uint16 is a `uint16` that can be unset
2252
type Uint16 = Var[uint16]
2353

54+
// NewUint16 creates a new Uint16 with the given value
55+
func NewUint16(val uint16) Uint16 {
56+
return NewVar(val)
57+
}
58+
2459
// Uint32 is a `uint32` that can be unset
2560
type Uint32 = Var[uint32]
2661

62+
// NewUint32 creates a new Uint32 with the given value
63+
func NewUint32(val uint32) Uint32 {
64+
return NewVar(val)
65+
}
66+
2767
// Uint64 is a `uint64` that can be unset
2868
type Uint64 = Var[uint64]
2969

70+
// NewUint64 creates a new Uint64 with the given value
71+
func NewUint64(val uint64) Uint64 {
72+
return NewVar(val)
73+
}
74+
3075
// Byte is a `byte` that can be unset
3176
type Byte = Var[byte]
3277

78+
// NewByte creates a new Byte with the given value
79+
func NewByte(val byte) Byte {
80+
return NewVar(val)
81+
}
82+
3383
// Rune is a `rune` that can be unset
3484
type Rune = Var[rune]
3585

86+
// NewRune creates a new Rune with the given value
87+
func NewRune(val rune) Rune {
88+
return NewVar(val)
89+
}
90+
3691
// Complex64 is a `complex64` that can be unset
3792
type Complex64 = Var[complex64]
3893

94+
// NewComplex64 creates a new Complex64 with the given value
95+
func NewComplex64(val complex64) Complex64 {
96+
return NewVar(val)
97+
}
98+
3999
// Complex128 is a `complex128` that can be unset
40100
type Complex128 = Var[complex128]
41101

102+
// NewComplex128 creates a new Complex128 with the given value
103+
func NewComplex128(val complex128) Complex128 {
104+
return NewVar(val)
105+
}
106+
42107
// Int is an `int` that can be unset
43108
type Int = Var[int]
44109

110+
// NewInt creates a new Int with the given value
111+
func NewInt(val int) Int {
112+
return NewVar(val)
113+
}
114+
45115
// Int8 is an `int8` that can be unset
46116
type Int8 = Var[int8]
47117

118+
// NewInt8 creates a new Int8 with the given value
119+
func NewInt8(val int8) Int8 {
120+
return NewVar(val)
121+
}
122+
48123
// Int16 is an `int16` that can be unset
49124
type Int16 = Var[int16]
50125

126+
// NewInt16 creates a new Int16 with the given value
127+
func NewInt16(val int16) Int16 {
128+
return NewVar(val)
129+
}
130+
51131
// Int32 is an `int32` that can be unset
52132
type Int32 = Var[int32]
53133

134+
// NewInt32 creates a new Int32 with the given value
135+
func NewInt32(val int32) Int32 {
136+
return NewVar(val)
137+
}
138+
54139
// Int64 is an `int64` that can be unset
55140
type Int64 = Var[int64]
56141

142+
// NewInt64 creates a new Int64 with the given value
143+
func NewInt64(val int64) Int64 {
144+
return NewVar(val)
145+
}
146+
57147
// Var is a variable that can be set, unset and queried for its state.
58148
type Var[T any] struct {
59149
val T
@@ -83,3 +173,8 @@ func (v *Var[T]) Unset() {
83173
var temp T
84174
v.val = temp
85175
}
176+
177+
// NewVar creates a new Var with the given value
178+
func NewVar[T any](val T) Var[T] {
179+
return Var[T]{val: val, set: true}
180+
}

0 commit comments

Comments
 (0)