Compare commits
1 Commits
main
...
a9a9b4d9bb
Author | SHA1 | Date | |
---|---|---|---|
a9a9b4d9bb |
@ -1,40 +0,0 @@
|
|||||||
name: Validate the build
|
|
||||||
run-name: ${{ gitea.actor }} is validating
|
|
||||||
on: [push]
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
validate-build:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
container:
|
|
||||||
image: node:current-alpine
|
|
||||||
steps:
|
|
||||||
- name: Install dependencies
|
|
||||||
run: |
|
|
||||||
echo "https://dl-cdn.alpinelinux.org/alpine/edge/main" >> /etc/apk/repositories
|
|
||||||
echo "https://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories
|
|
||||||
apk update
|
|
||||||
apk add --no-cache git make bash go
|
|
||||||
|
|
||||||
GOBIN=/usr/local/bin go install mvdan.cc/gofumpt@latest
|
|
||||||
|
|
||||||
export "PATH=$PATH:/root/go/bin"
|
|
||||||
|
|
||||||
echo "---------------------"
|
|
||||||
echo "Go version:"
|
|
||||||
go version
|
|
||||||
echo "---------------------"
|
|
||||||
|
|
||||||
- name: Check out repository code
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- name: Fetch dependencies
|
|
||||||
run: |
|
|
||||||
go mod download
|
|
||||||
|
|
||||||
- name: Validate the code and formatting
|
|
||||||
run: |
|
|
||||||
make validate
|
|
||||||
|
|
||||||
- name: Run tests
|
|
||||||
run: |
|
|
||||||
make test
|
|
41
internal/util/option/option.go
Normal file
41
internal/util/option/option.go
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
package option
|
||||||
|
|
||||||
|
type Option[T any] struct {
|
||||||
|
hasValue bool
|
||||||
|
value T
|
||||||
|
}
|
||||||
|
|
||||||
|
func Some[T any](value T) Option[T] {
|
||||||
|
return Option[T]{
|
||||||
|
hasValue: true,
|
||||||
|
value: value,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func None[T any]() Option[T] {
|
||||||
|
return Option[T]{
|
||||||
|
hasValue: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o Option[T]) IsSome() bool {
|
||||||
|
return o.hasValue
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o Option[T]) IsNone() bool {
|
||||||
|
return !o.hasValue
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o Option[T]) Value() T {
|
||||||
|
if !o.hasValue {
|
||||||
|
panic("Option has no value")
|
||||||
|
}
|
||||||
|
return o.value
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o Option[T]) ValueOr(defaultValue T) T {
|
||||||
|
if !o.hasValue {
|
||||||
|
return defaultValue
|
||||||
|
}
|
||||||
|
return o.value
|
||||||
|
}
|
54
internal/util/option/option_test.go
Normal file
54
internal/util/option/option_test.go
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
package option_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
. "git.omicron.one/omicron/linkshare/internal/util/option"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSome(t *testing.T) {
|
||||||
|
opt := Some(42)
|
||||||
|
|
||||||
|
if !opt.IsSome() {
|
||||||
|
t.Error("Expected IsSome() to be true for Some(42)")
|
||||||
|
}
|
||||||
|
|
||||||
|
if opt.IsNone() {
|
||||||
|
t.Error("Expected IsNone() to be false for Some(42)")
|
||||||
|
}
|
||||||
|
|
||||||
|
if opt.Value() != 42 {
|
||||||
|
t.Errorf("Expected Value() to be 42, got %v", opt.Value())
|
||||||
|
}
|
||||||
|
|
||||||
|
if opt.ValueOr(0) != 42 {
|
||||||
|
t.Errorf("Expected ValueOr(0) to be 42, got %v", opt.ValueOr(0))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNone(t *testing.T) {
|
||||||
|
opt := None[int]()
|
||||||
|
|
||||||
|
if opt.IsSome() {
|
||||||
|
t.Error("Expected IsSome() to be false for None[int]()")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !opt.IsNone() {
|
||||||
|
t.Error("Expected IsNone() to be true for None[int]()")
|
||||||
|
}
|
||||||
|
|
||||||
|
if opt.ValueOr(99) != 99 {
|
||||||
|
t.Errorf("Expected ValueOr(99) to be 99, got %v", opt.ValueOr(99))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPanic(t *testing.T) {
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r == nil {
|
||||||
|
t.Error("Expected Value() to panic on None")
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
opt := None[string]()
|
||||||
|
_ = opt.Value() // This should panic
|
||||||
|
}
|
Reference in New Issue
Block a user