Skip to content

Commit

Permalink
std: refactor optional cast to package
Browse files Browse the repository at this point in the history
  • Loading branch information
pulsejet committed Feb 3, 2025
1 parent 57549fb commit 2be2ce7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
4 changes: 2 additions & 2 deletions std/ndn/spec_2022/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (d *Data) Name() enc.Name {

func (d *Data) ContentType() (val optional.Optional[ndn.ContentType]) {
if d.MetaInfo != nil {
return utils.ConvIntOpt[uint64, ndn.ContentType](d.MetaInfo.ContentType)
return optional.CastInt[uint64, ndn.ContentType](d.MetaInfo.ContentType)
}
return val
}
Expand Down Expand Up @@ -233,7 +233,7 @@ func (Spec) MakeData(name enc.Name, config *ndn.DataConfig, content enc.Wire, si
data := &Data{
NameV: name,
MetaInfo: &MetaInfo{
ContentType: utils.ConvIntOpt[ndn.ContentType, uint64](config.ContentType),
ContentType: optional.CastInt[ndn.ContentType, uint64](config.ContentType),
FreshnessPeriod: config.Freshness,
FinalBlockID: finalBlock,
},
Expand Down
19 changes: 19 additions & 0 deletions std/types/optional/optional.go
Original file line number Diff line number Diff line change
@@ -1,45 +1,64 @@
package optional

import "golang.org/x/exp/constraints"

// Optional is a type that represents an optional value
type Optional[T any] struct {
value T
isSet bool
}

// IsSet returns true if the optional value is set
func (o Optional[T]) IsSet() bool {
return o.isSet
}

// Set sets the optional value
func (o *Optional[T]) Set(v T) {
o.value = v
o.isSet = true
}

// Unset unsets the optional value
func (o *Optional[T]) Unset() {
o.isSet = false
}

// Get returns the optional value and a boolean indicating if the value is set
func (o Optional[T]) Get() (T, bool) {
return o.value, o.isSet
}

// GetOr returns the optional value or a default value if the value is not set
func (o Optional[T]) GetOr(def T) T {
if o.isSet {
return o.value
}
return def
}

// Unwrap returns the optional value or panics if the value is not set
func (o Optional[T]) Unwrap() T {
if o.isSet {
return o.value
}
panic("Optional value is not set")
}

// Some creates an optional value with the given value
func Some[T any](v T) Optional[T] {
return Optional[T]{value: v, isSet: true}
}

// None creates an optional value with no value set
func None[T any]() Optional[T] {
return Optional[T]{isSet: false}
}

// CastInt converts an integer optional value to another type
func CastInt[A, B constraints.Integer](a Optional[A]) (out Optional[B]) {
if a.IsSet() {
out.Set(B(a.Unwrap()))
}
return out
}
8 changes: 0 additions & 8 deletions std/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,6 @@ func ConvIntPtr[A, B constraints.Integer](a *A) *B {
}
}

// ConvOptional converts an optional value to another type
func ConvIntOpt[A, B constraints.Integer](a optional.Optional[A]) (out optional.Optional[B]) {
if a.IsSet() {
out.Set(B(a.Unwrap()))
}
return out
}

func MakeTimestamp(t time.Time) uint64 {
return uint64(t.UnixNano() / int64(time.Millisecond))
}
Expand Down

0 comments on commit 2be2ce7

Please sign in to comment.