Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move to Go 1.21.x #10

Merged
merged 1 commit into from
Jul 15, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: "1.22.x"
go-version: "1.21.x"
- name: Install dependencies
run: go get .
- name: Vet
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/restatedev/sdk-go

go 1.22.0
go 1.21.0

require (
github.com/golang-jwt/jwt/v5 v5.2.1
Expand Down
33 changes: 28 additions & 5 deletions internal/rand/rand.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ package rand
import (
"crypto/sha256"
"encoding/binary"
"math/rand/v2"

"github.com/google/uuid"
)

type Rand struct {
*rand.Rand
source *Source
}

func New(invocationID []byte) *Rand {
return &Rand{rand.New(newSource(invocationID))}
return &Rand{newSource(invocationID)}
}

func (r *Rand) UUID() uuid.UUID {
Expand All @@ -25,6 +24,23 @@ func (r *Rand) UUID() uuid.UUID {
return uuid
}

func (r *Rand) Float64() float64 {
// use the math/rand/v2 implementation of Float64() which is more correct
// and also matches our TS implementation
return float64(r.Uint64()<<11>>11) / (1 << 53)
}

func (r *Rand) Uint64() uint64 {
return r.source.Uint64()
}

// Source returns a deterministic random source that can be provided to math/rand.New()
// and math/rand/v2.New(). The v2 version of rand is strongly recommended where Go 1.22
// is used, and once this library begins to depend on 1.22, it will be embedded in Rand.
func (r *Rand) Source() *Source {
return r.source
}

type Source struct {
state [4]uint64
}
Expand All @@ -43,6 +59,15 @@ func newSource(invocationID []byte) *Source {
}}
}

func (s *Source) Int63() int64 {
return int64(s.Uint64() & ((1 << 63) - 1))
}

// only the v1 rand package has this method
func (s *Source) Seed(int64) {
panic("The Restate random source is already deterministic based on invocation ID and must not be seeded")
}

func (s *Source) Uint64() uint64 {
result := rotl((s.state[0]+s.state[3]), 23) + s.state[0]

Expand All @@ -63,5 +88,3 @@ func (s *Source) Uint64() uint64 {
func rotl(x uint64, k uint64) uint64 {
return (x << k) | (x >> (64 - k))
}

var _ rand.Source = (*Source)(nil)
5 changes: 2 additions & 3 deletions internal/rand/rand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package rand

import (
"encoding/hex"
"math/rand/v2"
"testing"
)

Expand Down Expand Up @@ -35,7 +34,7 @@ func TestUint64(t *testing.T) {

func TestFloat64(t *testing.T) {
source := &Source{state: [4]uint64{1, 2, 3, 4}}
rand := &Rand{rand.New(source)}
rand := &Rand{source}

expected := []float64{
4.656612984099695e-9, 6.519269457605503e-9, 0.39843750651926946,
Expand All @@ -53,7 +52,7 @@ func TestFloat64(t *testing.T) {

func TestUUID(t *testing.T) {
source := &Source{state: [4]uint64{1, 2, 3, 4}}
rand := &Rand{rand.New(source)}
rand := &Rand{source}

expected := []string{
"01008002-0000-4000-a700-800300000000",
Expand Down
8 changes: 4 additions & 4 deletions reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ type serviceNamer interface {
}

var (
typeOfContext = reflect.TypeFor[Context]()
typeOfObjectContext = reflect.TypeFor[ObjectContext]()
typeOfVoid = reflect.TypeFor[Void]()
typeOfError = reflect.TypeFor[error]()
typeOfContext = reflect.TypeOf((*Context)(nil)).Elem()
typeOfObjectContext = reflect.TypeOf((*ObjectContext)(nil)).Elem()
typeOfVoid = reflect.TypeOf((*Void)(nil))
typeOfError = reflect.TypeOf((*error)(nil))
)

// Object converts a struct with methods into a Virtual Object where each correctly-typed
Expand Down
Loading