mirror of
https://github.com/temporalio/temporal.git
synced 2026-08-30 18:41:49 -07:00
## What changed? Follow up to #7761: 1. Use more concrete types instead of `comparable`. 2. Use `softassert` for "compile" time errors. 3. Rename `chasm.Collection` to `chasm.Map` but left proto `CollectionAttributes` intact. This will allow to add support for other collection type in future (slice, array). ## Why? It is better to narrow key type as much as possible. Other types are not supported anyway. ## How did you test it? - [ ] built - [ ] run locally and tested manually - [x] covered by existing tests - [x] added new unit test(s) - [ ] added new functional test(s)
54 lines
1.3 KiB
Go
54 lines
1.3 KiB
Go
package chasm
|
||
|
||
import (
|
||
"go/ast"
|
||
"go/parser"
|
||
"go/printer"
|
||
"go/token"
|
||
"path/filepath"
|
||
"runtime"
|
||
"strings"
|
||
"testing"
|
||
|
||
"github.com/stretchr/testify/require"
|
||
)
|
||
|
||
// Another approach would be to code generate string const.
|
||
func TestMapKeyTypesMatchConst(t *testing.T) {
|
||
_, currentFile, _, ok := runtime.Caller(0)
|
||
require.True(t, ok, "failed to get current file path")
|
||
srcFile := filepath.Join(filepath.Dir(currentFile), "map.go")
|
||
|
||
fset := token.NewFileSet()
|
||
file, err := parser.ParseFile(fset, srcFile, nil, parser.AllErrors)
|
||
require.NoError(t, err)
|
||
|
||
var found string
|
||
// Walk the top‐level declarations looking for:
|
||
// type Map[K ... , T any] map[K]T
|
||
for _, decl := range file.Decls {
|
||
gd, ok := decl.(*ast.GenDecl)
|
||
if !ok || gd.Tok != token.TYPE {
|
||
continue
|
||
}
|
||
for _, spec := range gd.Specs {
|
||
ts, ok := spec.(*ast.TypeSpec)
|
||
if !ok || ts.Name.Name != "Map" {
|
||
continue
|
||
}
|
||
// ts.TypeParams.List[0] is the field for K
|
||
if ts.TypeParams != nil && len(ts.TypeParams.List) > 0 {
|
||
field := ts.TypeParams.List[0]
|
||
var buf strings.Builder
|
||
// pretty‐print the AST node for the constraint
|
||
err = printer.Fprint(&buf, fset, field.Type)
|
||
require.NoError(t, err)
|
||
found = buf.String()
|
||
}
|
||
}
|
||
}
|
||
|
||
require.NotEmpty(t, found, "could not locate Map[K …] in AST")
|
||
require.Equal(t, mapKeyTypes, found)
|
||
}
|