From af0a612eca25fbb56d3c8885965f43bd7e106eda Mon Sep 17 00:00:00 2001 From: diamondburned Date: Tue, 6 Aug 2024 13:32:02 +0700 Subject: [PATCH] json/option: Add helper Optional[T] type --- go.mod | 2 +- utils/json/option/option.go | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index fd0635b..84609eb 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/diamondburned/arikawa/v3 -go 1.16 +go 1.18 require ( github.com/gorilla/schema v1.4.1 diff --git a/utils/json/option/option.go b/utils/json/option/option.go index f192c28..95dd712 100644 --- a/utils/json/option/option.go +++ b/utils/json/option/option.go @@ -4,3 +4,17 @@ // To generate pointerrized primitives, there are helper functions NewT() for // each option type. package option + +// Optional wraps a type to make it omittable. +type Optional[T any] *T + +// Some creates a new Optional with the value of the passed type. +func Some[T any](v T) Optional[T] { + return &v +} + +// PtrTo creates a pointer to the passed value. +// It is like [Some], except it returns *T directly rather than Optional[T]. +func PtrTo[T any](v T) *T { + return &v +}