Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ linters:

exclusions:
rules:
- path-except: cmd/generator/*.go
- path: cmd/generator/
linters:
- forbidigo # fmt functions are not forbidden here
- gochecknoglobals # global variables are not forbidden here
- path: _test.go
linters:
- wrapcheck

settings:
godot:
Expand Down
2 changes: 2 additions & 0 deletions bool_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions byte_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions bytes_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions cmd/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ type {{.Name}} struct {
exists bool
}

var _ commonInterface[{{.Type}}] = (*{{.Name}})(nil)

// Some{{.Name}} creates an optional {{.Name}} with the given {{.Type}} value.
// The returned {{.Name}} will have IsSome() == true and IsZero() == false.
//
Expand Down
2 changes: 1 addition & 1 deletion doc.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Package option provides a type-safe way to represent optional values in Go.
// An Optional[T] can either contain a value of type T (Some) or be empty (None).
// A Generic[T] can either contain a value of type T (Some) or be empty (None).
//
// This is useful for:
// - Clearly representing nullable fields in structs.
Expand Down
24 changes: 24 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func newDecodeWithCodeError(operationType string, code byte) error {
}
}

func getGenericTypeName[T any]() string {
return fmt.Sprintf("Generic[%T]", zero[T]())
}

func newDecodeError(operationType string, err error) error {
if err == nil {
return nil
Expand All @@ -40,6 +44,18 @@ func newDecodeError(operationType string, err error) error {
}
}

func newDecodeGenericError[T any](err error) error {
if err == nil {
return nil
}

return DecodeError{
Type: getGenericTypeName[T](),
Code: NoneByte(),
Parent: err,
}
}

// EncodeError is returned when encoding failed due to stream errors.
type EncodeError struct {
Type string
Expand All @@ -58,3 +74,11 @@ func newEncodeError(operationType string, err error) error {

return EncodeError{Type: operationType, Parent: err}
}

func newEncodeGenericError[T any](err error) error {
if err == nil {
return nil
}

return EncodeError{Type: getGenericTypeName[T](), Parent: err}
}
39 changes: 37 additions & 2 deletions errors_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package option //nolint:testpackage

// this is unit test, that checks internal logic of error constructing.

import (
Expand All @@ -13,7 +14,7 @@ var (
errTest = errors.New("some error")
)

func TestDecodeError_Error(t *testing.T) {
func TestEncodeError_Error(t *testing.T) {
t.Parallel()

t.Run("newEncodeError with error", func(t *testing.T) {
Expand All @@ -32,9 +33,26 @@ func TestDecodeError_Error(t *testing.T) {

require.NoError(t, a)
})

t.Run("newEncoderGenericError", func(t *testing.T) {
t.Parallel()

a := newEncodeGenericError[int](errTest)

require.Error(t, a)
assert.Equal(t, "failed to encode Generic[int]: some error", a.Error())
})

t.Run("newEncodeGenericError without error", func(t *testing.T) {
t.Parallel()

a := newEncodeGenericError[int](nil)

require.NoError(t, a)
})
}

func TestEncodeError_Error(t *testing.T) {
func TestDecodeError_Error(t *testing.T) {
t.Parallel()

t.Run("newDecodeError with error", func(t *testing.T) {
Expand Down Expand Up @@ -62,4 +80,21 @@ func TestEncodeError_Error(t *testing.T) {
require.Error(t, a)
assert.Equal(t, "failed to decode Byte, invalid code: 1", a.Error())
})

t.Run("newDecodeGenericError", func(t *testing.T) {
t.Parallel()

a := newDecodeGenericError[int](errTest)

require.Error(t, a)
assert.Equal(t, "failed to decode Generic[int]: some error", a.Error())
})

t.Run("newDecodeGenericError without error", func(t *testing.T) {
t.Parallel()

a := newDecodeGenericError[int](nil)

require.NoError(t, a)
})
}
2 changes: 2 additions & 0 deletions float32_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions float64_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading