Skip to content

Commit c6dae6c

Browse files
committed
tlv: rename receivers, add ValOpt
ValOpt returns an Option of the underlying value.
1 parent 956b00b commit c6dae6c

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

tlv/record_type.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,15 @@ type OptionalRecordT[T TlvType, V any] struct {
104104
// TlvType returns the type of the record. This is the value used to identify
105105
// this type on the wire. This value is bound to the specified TlvType type
106106
// param.
107-
func (t *OptionalRecordT[T, V]) TlvType() Type {
107+
func (o *OptionalRecordT[T, V]) TlvType() Type {
108108
zeroRecord := ZeroRecordT[T, V]()
109109
return zeroRecord.TlvType()
110110
}
111111

112112
// WhenSomeV executes the given function if the optional record is present.
113113
// This operates on the inner most type, V, which is the value of the record.
114-
func (t *OptionalRecordT[T, V]) WhenSomeV(f func(V)) {
115-
t.Option.WhenSome(func(r RecordT[T, V]) {
114+
func (o *OptionalRecordT[T, V]) WhenSomeV(f func(V)) {
115+
o.Option.WhenSome(func(r RecordT[T, V]) {
116116
f(r.Val)
117117
})
118118
}
@@ -126,7 +126,7 @@ func (o *OptionalRecordT[T, V]) UnwrapOrFailV(t *testing.T) V {
126126
return inner.Val
127127
}
128128

129-
// UnwrapOrErr is used to extract a value from an option, if the option is
129+
// UnwrapOrErrV is used to extract a value from an option, if the option is
130130
// empty, then the specified error is returned directly. This gives the
131131
// underlying value of the record, instead of the record itself.
132132
func (o *OptionalRecordT[T, V]) UnwrapOrErrV(err error) (V, error) {
@@ -141,10 +141,19 @@ func (o *OptionalRecordT[T, V]) UnwrapOrErrV(err error) (V, error) {
141141
}
142142

143143
// Zero returns a zero value of the record type.
144-
func (t *OptionalRecordT[T, V]) Zero() RecordT[T, V] {
144+
func (o *OptionalRecordT[T, V]) Zero() RecordT[T, V] {
145145
return ZeroRecordT[T, V]()
146146
}
147147

148+
// ValOpt returns an Option of the underlying value. This can be used to chain
149+
// other option related methods to avoid needing to first go through the outer
150+
// record.
151+
func (o *OptionalRecordT[T, V]) ValOpt() fn.Option[V] {
152+
return fn.MapOption(func(record RecordT[T, V]) V {
153+
return record.Val
154+
})(o.Option)
155+
}
156+
148157
// SomeRecordT creates a new OptionalRecordT type from a given RecordT type.
149158
func SomeRecordT[T TlvType, V any](record RecordT[T, V]) OptionalRecordT[T, V] {
150159
return OptionalRecordT[T, V]{
@@ -181,8 +190,8 @@ func (b BigSizeT[T]) Int() T {
181190
}
182191

183192
// Record returns the underlying record interface for the record type.
184-
func (t *BigSizeT[T]) Record() Record {
193+
func (b *BigSizeT[T]) Record() Record {
185194
// We use a zero value for the type here as this should be used with
186195
// the higher order RecordT type.
187-
return MakeBigSizeRecord(0, &t.v)
196+
return MakeBigSizeRecord(0, &b.v)
188197
}

0 commit comments

Comments
 (0)