tlv: rename receivers, add ValOpt

ValOpt returns an Option of the underlying value.
This commit is contained in:
Oliver Gugger 2024-04-25 16:51:12 +02:00
parent 956b00b599
commit c6dae6c3c0
No known key found for this signature in database
GPG Key ID: 8E4256593F177720

View File

@ -104,15 +104,15 @@ type OptionalRecordT[T TlvType, V any] struct {
// TlvType returns the type of the record. This is the value used to identify // TlvType returns the type of the record. This is the value used to identify
// this type on the wire. This value is bound to the specified TlvType type // this type on the wire. This value is bound to the specified TlvType type
// param. // param.
func (t *OptionalRecordT[T, V]) TlvType() Type { func (o *OptionalRecordT[T, V]) TlvType() Type {
zeroRecord := ZeroRecordT[T, V]() zeroRecord := ZeroRecordT[T, V]()
return zeroRecord.TlvType() return zeroRecord.TlvType()
} }
// WhenSomeV executes the given function if the optional record is present. // WhenSomeV executes the given function if the optional record is present.
// This operates on the inner most type, V, which is the value of the record. // This operates on the inner most type, V, which is the value of the record.
func (t *OptionalRecordT[T, V]) WhenSomeV(f func(V)) { func (o *OptionalRecordT[T, V]) WhenSomeV(f func(V)) {
t.Option.WhenSome(func(r RecordT[T, V]) { o.Option.WhenSome(func(r RecordT[T, V]) {
f(r.Val) f(r.Val)
}) })
} }
@ -126,7 +126,7 @@ func (o *OptionalRecordT[T, V]) UnwrapOrFailV(t *testing.T) V {
return inner.Val return inner.Val
} }
// UnwrapOrErr is used to extract a value from an option, if the option is // UnwrapOrErrV is used to extract a value from an option, if the option is
// empty, then the specified error is returned directly. This gives the // empty, then the specified error is returned directly. This gives the
// underlying value of the record, instead of the record itself. // underlying value of the record, instead of the record itself.
func (o *OptionalRecordT[T, V]) UnwrapOrErrV(err error) (V, error) { func (o *OptionalRecordT[T, V]) UnwrapOrErrV(err error) (V, error) {
@ -141,10 +141,19 @@ func (o *OptionalRecordT[T, V]) UnwrapOrErrV(err error) (V, error) {
} }
// Zero returns a zero value of the record type. // Zero returns a zero value of the record type.
func (t *OptionalRecordT[T, V]) Zero() RecordT[T, V] { func (o *OptionalRecordT[T, V]) Zero() RecordT[T, V] {
return ZeroRecordT[T, V]() return ZeroRecordT[T, V]()
} }
// ValOpt returns an Option of the underlying value. This can be used to chain
// other option related methods to avoid needing to first go through the outer
// record.
func (o *OptionalRecordT[T, V]) ValOpt() fn.Option[V] {
return fn.MapOption(func(record RecordT[T, V]) V {
return record.Val
})(o.Option)
}
// SomeRecordT creates a new OptionalRecordT type from a given RecordT type. // SomeRecordT creates a new OptionalRecordT type from a given RecordT type.
func SomeRecordT[T TlvType, V any](record RecordT[T, V]) OptionalRecordT[T, V] { func SomeRecordT[T TlvType, V any](record RecordT[T, V]) OptionalRecordT[T, V] {
return OptionalRecordT[T, V]{ return OptionalRecordT[T, V]{
@ -181,8 +190,8 @@ func (b BigSizeT[T]) Int() T {
} }
// Record returns the underlying record interface for the record type. // Record returns the underlying record interface for the record type.
func (t *BigSizeT[T]) Record() Record { func (b *BigSizeT[T]) Record() Record {
// We use a zero value for the type here as this should be used with // We use a zero value for the type here as this should be used with
// the higher order RecordT type. // the higher order RecordT type.
return MakeBigSizeRecord(0, &t.v) return MakeBigSizeRecord(0, &b.v)
} }