Files
temporal/chasm/map_test.go
Alex Shtin 10eb24461a CHASM: follow up collection changes (#7795)
## 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)
2025-05-29 14:03:03 +00:00

54 lines
1.3 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 toplevel 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
// prettyprint 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)
}