From 807a25b638e38d7c0446bb728412fb16fd035035 Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Sat, 4 Jan 2025 15:08:53 +0100 Subject: init --- options/options.go | 22 ++++++++++++++++++++++ options/options_test.go | 19 +++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 options/options.go create mode 100644 options/options_test.go (limited to 'options') diff --git a/options/options.go b/options/options.go new file mode 100644 index 0000000..e058559 --- /dev/null +++ b/options/options.go @@ -0,0 +1,22 @@ +/******************************************************************************* +* Copyright 2025 Stefan Majewsky +* SPDX-License-Identifier: Apache-2.0 +* Refer to the file "LICENSE" for details. +*******************************************************************************/ + +// Package options provides additional functions for type option.Option +// that cannot be expressed as methods on the Option type itself. +package options + +import . "github.com/majewsky/gg/option" + +// NOTE: Keep functions sorted by name. + +// FromPointer converts a *T into an Option[T]. +func FromPointer[T any](value *T) Option[T] { + if value == nil { + return None[T]() + } else { + return Some(*value) + } +} diff --git a/options/options_test.go b/options/options_test.go new file mode 100644 index 0000000..69a4b43 --- /dev/null +++ b/options/options_test.go @@ -0,0 +1,19 @@ +/******************************************************************************* +* Copyright 2025 Stefan Majewsky +* SPDX-License-Identifier: Apache-2.0 +* Refer to the file "LICENSE" for details. +*******************************************************************************/ + +package options + +import ( + "testing" + + . "github.com/majewsky/gg/internal/test" + . "github.com/majewsky/gg/option" +) + +func TestFromPointer(t *testing.T) { + AssertEqual(t, FromPointer[int](nil), None[int]()) + AssertEqual(t, FromPointer(PointerTo[int](42)), Some(42)) +} -- cgit v1.2.3