feat: tipo suma Result[T] con Ok y Err
Primer tipo del registry: Result generico inspirado en Rust. Tipo suma con dos variantes: Ok(T) y Err(error). Metodos: IsOk, IsErr, Unwrap, UnwrapErr, UnwrapOr. Incluye implementacion, documentacion .md y 4 tests. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -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,21 @@
|
||||
---
|
||||
name: result
|
||||
lang: go
|
||||
domain: core
|
||||
version: "1.0.0"
|
||||
algebraic: sum
|
||||
definition: |
|
||||
type Result[T any] struct {
|
||||
ok *T
|
||||
err error
|
||||
}
|
||||
description: "Tipo suma generico que representa exito (Ok) o fallo (Err). Permite componer operaciones que pueden fallar sin recurrir a multiples returns (T, error)."
|
||||
tags: [result, sum, error-handling, functional, generic]
|
||||
uses_types: []
|
||||
file_path: "types/core/result.go"
|
||||
---
|
||||
|
||||
## Notas
|
||||
|
||||
Tipo suma con dos variantes: Ok(T) y Err(error). Inspirado en Result de Rust.
|
||||
Util para encadenar operaciones y evitar el patron `if err != nil` repetitivo.
|
||||
@@ -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