Skip to content

Commit 6ce1ce3

Browse files
author
Shubham Chaturvedi
committed
feat: StandardLibrary for Go
1 parent 9e195a1 commit 6ce1ce3

14 files changed

Lines changed: 493 additions & 2 deletions

File tree

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# This workflow performs tests in Go.
2+
name: Library Go tests
3+
4+
on:
5+
workflow_call:
6+
inputs:
7+
dafny:
8+
description: "The Dafny version to run"
9+
required: true
10+
type: string
11+
regenerate-code:
12+
description: "Regenerate code using smithy-dafny"
13+
required: false
14+
default: false
15+
type: boolean
16+
17+
jobs:
18+
testGo:
19+
strategy:
20+
fail-fast: false
21+
matrix:
22+
library:
23+
[
24+
StandardLibrary,
25+
]
26+
go-version: ["1.23"]
27+
os: [
28+
# TODO fix Dafny-generated tests on Windows;
29+
# the sys.path workaround for generated Dafny doesn't work on Windows.
30+
# Note: only tests use the sys.path workaround, not source code.
31+
# Windows source code is tested downstream (ex. ESDK-Go CI).
32+
# windows-latest,
33+
ubuntu-latest,
34+
macos-12,
35+
]
36+
runs-on: ${{ matrix.os }}
37+
defaults:
38+
run:
39+
shell: bash
40+
permissions:
41+
id-token: write
42+
contents: read
43+
steps:
44+
- name: Support longpaths on Git checkout
45+
run: |
46+
git config --global core.longpaths true
47+
48+
- name: Configure AWS Credentials
49+
uses: aws-actions/configure-aws-credentials@v4
50+
with:
51+
aws-region: us-west-2
52+
role-to-assume: arn:aws:iam::370957321024:role/GitHub-CI-MPL-Dafny-Role-us-west-2
53+
role-session-name: GoTests
54+
55+
- uses: actions/checkout@v4
56+
# The specification submodule is private so we don't have access, but we don't need
57+
# it to verify the Dafny code. Instead we manually pull the submodules we DO need.
58+
- run: git submodule update --init libraries
59+
- run: git submodule update --init smithy-dafny
60+
61+
# TODO: This is still in nighlty
62+
- name: Setup Dafny
63+
uses: dafny-lang/setup-dafny-action@v1.7.0
64+
with:
65+
dafny-version: "nightly-latest"
66+
67+
- name: Setup Go ${{ matrix.go-version }} for running tests
68+
uses: actions/setup-go@v2
69+
with:
70+
go-version: ${{ matrix.go-version }}
71+
72+
- name: Install Go imports
73+
run: |
74+
go install golang.org/x/tools/cmd/goimports@latest
75+
76+
- name: Build ${{ matrix.library }} implementation
77+
working-directory: ./${{ matrix.library }}
78+
run: |
79+
# This works because `node` is installed by default on GHA runners
80+
CORES=$(node -e 'console.log(os.cpus().length)')
81+
make transpile_go CORES=$CORES
82+
83+
- name: Test ${{ matrix.library }}
84+
working-directory: ./${{ matrix.library }}
85+
shell: bash
86+
run: |
87+
make test_go

StandardLibrary/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ transpile_implementation: STD_LIBRARY=
1717
transpile_test: STD_LIBRARY=
1818
transpile_dependencies: STD_LIBRARY=
1919

20-
# Python
20+
GO_MODULE_NAME="github.com/dafny-lang/DafnyStandardLibGo"
2121

22+
# Python
2223
PYTHON_MODULE_NAME=smithy_dafny_standard_library
2324

2425
RUST_OTHER_FILES := \
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package ConcurrentCall
2+
3+
import "sync"
4+
5+
func ConcurrentCall(calle Callee, serialIters uint32, concurrentIters uint32) {
6+
var wg sync.WaitGroup
7+
for i := uint32(0); i < concurrentIters; i++ {
8+
wg.Add(1)
9+
concurrentPos := i
10+
go func() {
11+
defer wg.Done()
12+
for j := uint32(0); j < serialIters; j++ {
13+
calle.Call(j, concurrentPos)
14+
}
15+
}()
16+
wg.Wait()
17+
}
18+
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package DafnyLibraries
2+
3+
import (
4+
sync "sync"
5+
6+
_dafny "github.com/dafny-lang/DafnyRuntimeGo/v4/dafny"
7+
Std_Wrappers "github.com/dafny-lang/DafnyStandardLibGo/Wrappers"
8+
)
9+
10+
// Definition of class MutableMap
11+
type MutableMap struct {
12+
Internal sync.Map
13+
}
14+
15+
func New_MutableMap_() *MutableMap {
16+
return &MutableMap{}
17+
}
18+
19+
type CompanionStruct_MutableMap_ struct {
20+
}
21+
22+
var Companion_MutableMap_ = CompanionStruct_MutableMap_{}
23+
24+
func (_this *MutableMap) Equals(other *MutableMap) bool {
25+
return _this == other
26+
}
27+
28+
func (_this *MutableMap) EqualsGeneric(x interface{}) bool {
29+
other, ok := x.(*MutableMap)
30+
return ok && _this.Equals(other)
31+
}
32+
33+
func (*MutableMap) String() string {
34+
return "ExternConcurrent.MutableMap"
35+
}
36+
37+
func Type_MutableMap_(Type_K_ _dafny.TypeDescriptor, Type_V_ _dafny.TypeDescriptor) _dafny.TypeDescriptor {
38+
return type_MutableMap_{Type_K_, Type_V_}
39+
}
40+
41+
type type_MutableMap_ struct {
42+
Type_K_ _dafny.TypeDescriptor
43+
Type_V_ _dafny.TypeDescriptor
44+
}
45+
46+
func (_this type_MutableMap_) Default() interface{} {
47+
return (*MutableMap)(nil)
48+
}
49+
50+
func (_this type_MutableMap_) String() string {
51+
return "ExternConcurrent.MutableMap"
52+
}
53+
func (_this *MutableMap) ParentTraits_() []*_dafny.TraitID {
54+
return [](*_dafny.TraitID){}
55+
}
56+
57+
var _ _dafny.TraitOffspring = &MutableMap{}
58+
59+
func (_this *MutableMap) Ctor__() {
60+
{
61+
}
62+
}
63+
func (_this *MutableMap) Keys() _dafny.Set {
64+
{
65+
keys := make([]interface{}, 0)
66+
67+
_this.Internal.Range(func(key, value interface{}) bool {
68+
keys = append(keys, key)
69+
return true
70+
})
71+
72+
return _dafny.SetOf(keys[:]...)
73+
}
74+
}
75+
func (_this *MutableMap) HasKey(k interface{}) bool {
76+
{
77+
_, ok := _this.Internal.Load(k)
78+
return ok
79+
}
80+
}
81+
func (_this *MutableMap) Values() _dafny.Set {
82+
{
83+
values := make([]interface{}, 0)
84+
85+
_this.Internal.Range(func(key, value interface{}) bool {
86+
values = append(values, value)
87+
return true
88+
})
89+
90+
return _dafny.SetOf(values[:]...)
91+
}
92+
}
93+
func (_this *MutableMap) Items() _dafny.Set {
94+
{
95+
items := make([]interface{}, 0)
96+
97+
_this.Internal.Range(func(key, value interface{}) bool {
98+
items = append(items, _dafny.TupleOf(key, value))
99+
return true
100+
})
101+
102+
return _dafny.SetOf(items[:]...)
103+
}
104+
}
105+
func (_this *MutableMap) Select(k interface{}) interface{} {
106+
r := _this.Get(k)
107+
if r.IsFailure() {
108+
panic("ff")
109+
}
110+
return r.Dtor_value()
111+
}
112+
func (_this *MutableMap) Get(k interface{}) Std_Wrappers.Option {
113+
{
114+
value, ok := _this.Internal.Load(k)
115+
if ok {
116+
return Std_Wrappers.Companion_Option_.Create_Some_(value)
117+
} else {
118+
return Std_Wrappers.Companion_Option_.Create_None_()
119+
}
120+
}
121+
}
122+
func (_this *MutableMap) Put(k interface{}, v interface{}) {
123+
{
124+
_this.Internal.Store(k, v)
125+
}
126+
}
127+
func (_this *MutableMap) Remove(k interface{}) {
128+
{
129+
_this.Internal.Delete(k)
130+
}
131+
}
132+
func (_this *MutableMap) Size() _dafny.Int {
133+
{
134+
var c _dafny.Int = _dafny.Zero
135+
136+
_this.Internal.Range(func(key, value interface{}) bool {
137+
c = c.Plus(_dafny.One)
138+
return true
139+
})
140+
141+
return c
142+
}
143+
}
144+
145+
// End of class MutableMap
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package FileIO
2+
3+
import (
4+
"io/ioutil"
5+
os "os"
6+
"path/filepath"
7+
8+
_dafny "github.com/dafny-lang/DafnyRuntimeGo/v4/dafny"
9+
)
10+
11+
func (_static CompanionStruct_Default___) INTERNAL_ReadBytesFromFile(path _dafny.Sequence) (isError bool, bytesRead _dafny.Sequence, errorMsg _dafny.Sequence) {
12+
p := _dafny.SequenceVerbatimString(path, false)
13+
dat, err := ioutil.ReadFile(p)
14+
if err != nil {
15+
errAsSequence := _dafny.UnicodeSeqOfUtf8Bytes(err.Error())
16+
return true, _dafny.EmptySeq, errAsSequence
17+
}
18+
datAsSequence := _dafny.SeqOfBytes(dat)
19+
return false, datAsSequence, _dafny.EmptySeq
20+
}
21+
22+
func (_static CompanionStruct_Default___) INTERNAL_WriteBytesToFile(path _dafny.Sequence, bytes _dafny.Sequence) (isError bool, errorMsg _dafny.Sequence) {
23+
p := _dafny.SequenceVerbatimString(path, false)
24+
25+
// Create directories
26+
os.MkdirAll(filepath.Dir(p), os.ModePerm)
27+
28+
bytesArray := _dafny.ToByteArray(bytes)
29+
err := ioutil.WriteFile(p, bytesArray, 0644)
30+
if err != nil {
31+
errAsSequence := _dafny.UnicodeSeqOfUtf8Bytes(err.Error())
32+
return true, errAsSequence
33+
}
34+
return false, _dafny.EmptySeq
35+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package SortedSets
2+
3+
import (
4+
"sort"
5+
6+
"github.com/dafny-lang/DafnyRuntimeGo/v4/dafny"
7+
)
8+
9+
func SetToOrderedSequence(set dafny.Set, fn func(interface{}, interface{}) bool) dafny.Sequence {
10+
var arr []interface{}
11+
for i := set.Iterator(); ; {
12+
val, ok := i()
13+
if ok {
14+
arr = append(arr, val)
15+
} else {
16+
sort.Slice(arr, func(i, j int) bool {
17+
a := arr[i].(dafny.Sequence)
18+
b := arr[j].(dafny.Sequence)
19+
for ii, jj := dafny.Iterate(a), dafny.Iterate(b); ; {
20+
v1, ok1 := ii()
21+
v2, ok2 := jj()
22+
if ok1 && ok2 {
23+
if fn(v1, v2) {
24+
return true
25+
}
26+
if fn(v2, v1) {
27+
return false
28+
}
29+
} else if ok1 {
30+
return false
31+
} else if ok2 {
32+
return true
33+
} else {
34+
return false
35+
}
36+
}
37+
})
38+
return dafny.SeqOf(arr...)
39+
}
40+
}
41+
}
42+
43+
func SetToOrderedSequence2(set dafny.Set, fn func(interface{}, interface{}) bool) dafny.Sequence {
44+
return SetToOrderedSequence(set, fn)
45+
}
46+
47+
func SetToSequence(set dafny.Set) dafny.Sequence {
48+
var arr []interface{}
49+
for i := set.Iterator(); ; {
50+
val, ok := i()
51+
if ok {
52+
arr = append(arr, val)
53+
} else {
54+
return dafny.SeqOf(arr...)
55+
}
56+
}
57+
58+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package _Time
2+
3+
import (
4+
"time"
5+
6+
"github.com/dafny-lang/DafnyRuntimeGo/v4/dafny"
7+
"github.com/dafny-lang/DafnyStandardLibGo/Wrappers"
8+
)
9+
10+
func CurrentRelativeTime() int64 {
11+
return int64(time.Now().Second())
12+
}
13+
14+
func GetCurrentTimeStamp() Wrappers.Result {
15+
return Wrappers.Companion_Result_.Create_Success_(dafny.SeqOfChars([]dafny.Char(time.Now().Format("2006-01-02T15:04:05.000000Z"))...))
16+
}
17+
18+
func CurrentRelativeTimeMilli() int64 {
19+
return time.Now().UnixMilli()
20+
}

0 commit comments

Comments
 (0)