refactor: mover .go de tipos Go a functions/{domain}/ para compilación unificada
Los archivos .go de tipos ahora viven junto a las funciones en functions/{domain}/
(mismo paquete Go), resolviendo errores de compilación por tipos no encontrados
(Option, Pair, Result, etc.). Los .md de metadata permanecen en types/{domain}/
con file_path actualizado a functions/. Se elimina types.go duplicado de infra.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
package core
|
||||
|
||||
// Error is the standard error type for impure functions in the registry.
|
||||
// Wraps a message string. Used as error_type reference.
|
||||
type Error struct {
|
||||
Message string
|
||||
}
|
||||
|
||||
func (e Error) Error() string {
|
||||
return e.Message
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package core
|
||||
|
||||
// Option represents a value that may or may not be present.
|
||||
// A sum type: Some(T) | None.
|
||||
type Option[T any] struct {
|
||||
value *T
|
||||
}
|
||||
|
||||
// Some creates an Option containing a value.
|
||||
func Some[T any](v T) Option[T] {
|
||||
return Option[T]{value: &v}
|
||||
}
|
||||
|
||||
// None creates an empty Option.
|
||||
func None[T any]() Option[T] {
|
||||
return Option[T]{}
|
||||
}
|
||||
|
||||
// IsSome returns true if the Option contains a value.
|
||||
func (o Option[T]) IsSome() bool {
|
||||
return o.value != nil
|
||||
}
|
||||
|
||||
// IsNone returns true if the Option is empty.
|
||||
func (o Option[T]) IsNone() bool {
|
||||
return o.value == nil
|
||||
}
|
||||
|
||||
// Unwrap returns the value or panics if None.
|
||||
func (o Option[T]) Unwrap() T {
|
||||
if o.value == nil {
|
||||
panic("called Unwrap on None")
|
||||
}
|
||||
return *o.value
|
||||
}
|
||||
|
||||
// UnwrapOr returns the value or a default if None.
|
||||
func (o Option[T]) UnwrapOr(def T) T {
|
||||
if o.value != nil {
|
||||
return *o.value
|
||||
}
|
||||
return def
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package core
|
||||
|
||||
// Pair holds two values of potentially different types.
|
||||
type Pair[A any, B any] struct {
|
||||
First A
|
||||
Second B
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package core
|
||||
|
||||
// Result is a sum type representing either a successful value or an error.
|
||||
// Exactly one of Ok or Err is non-nil at any time.
|
||||
type Result[T any] struct {
|
||||
ok *T
|
||||
err error
|
||||
}
|
||||
|
||||
// Ok creates a successful Result.
|
||||
func Ok[T any](v T) Result[T] {
|
||||
return Result[T]{ok: &v}
|
||||
}
|
||||
|
||||
// Err creates a failed Result.
|
||||
func Err[T any](err error) Result[T] {
|
||||
return Result[T]{err: err}
|
||||
}
|
||||
|
||||
// IsOk returns true if the Result contains a value.
|
||||
func (r Result[T]) IsOk() bool {
|
||||
return r.ok != nil
|
||||
}
|
||||
|
||||
// IsErr returns true if the Result contains an error.
|
||||
func (r Result[T]) IsErr() bool {
|
||||
return r.err != nil
|
||||
}
|
||||
|
||||
// Unwrap returns the value or panics if the Result is an error.
|
||||
func (r Result[T]) Unwrap() T {
|
||||
if r.ok == nil {
|
||||
panic("called Unwrap on an Err Result")
|
||||
}
|
||||
return *r.ok
|
||||
}
|
||||
|
||||
// UnwrapErr returns the error or panics if the Result is Ok.
|
||||
func (r Result[T]) UnwrapErr() error {
|
||||
if r.err == nil {
|
||||
panic("called UnwrapErr on an Ok Result")
|
||||
}
|
||||
return r.err
|
||||
}
|
||||
|
||||
// UnwrapOr returns the value or a default if the Result is an error.
|
||||
func (r Result[T]) UnwrapOr(def T) T {
|
||||
if r.ok != nil {
|
||||
return *r.ok
|
||||
}
|
||||
return def
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestResultOk(t *testing.T) {
|
||||
r := Ok(42)
|
||||
if !r.IsOk() {
|
||||
t.Error("expected IsOk")
|
||||
}
|
||||
if r.IsErr() {
|
||||
t.Error("expected not IsErr")
|
||||
}
|
||||
if r.Unwrap() != 42 {
|
||||
t.Errorf("got %d, want 42", r.Unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
func TestResultErr(t *testing.T) {
|
||||
r := Err[int](errors.New("fail"))
|
||||
if r.IsOk() {
|
||||
t.Error("expected not IsOk")
|
||||
}
|
||||
if !r.IsErr() {
|
||||
t.Error("expected IsErr")
|
||||
}
|
||||
if r.UnwrapErr().Error() != "fail" {
|
||||
t.Errorf("got %v", r.UnwrapErr())
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnwrapOr(t *testing.T) {
|
||||
ok := Ok(10)
|
||||
if ok.UnwrapOr(0) != 10 {
|
||||
t.Error("UnwrapOr on Ok should return value")
|
||||
}
|
||||
|
||||
err := Err[int](errors.New("fail"))
|
||||
if err.UnwrapOr(99) != 99 {
|
||||
t.Error("UnwrapOr on Err should return default")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnwrapPanics(t *testing.T) {
|
||||
defer func() {
|
||||
if recover() == nil {
|
||||
t.Error("Unwrap on Err should panic")
|
||||
}
|
||||
}()
|
||||
Err[int](errors.New("fail")).Unwrap()
|
||||
}
|
||||
Reference in New Issue
Block a user