@@ -7,6 +7,7 @@ package cmp
77import (
88 "fmt"
99 "reflect"
10+ "regexp"
1011 "strings"
1112
1213 "github.com/google/go-cmp/cmp/internal/function"
@@ -205,6 +206,11 @@ func (invalid) apply(s *state, _, _ reflect.Value) {
205206 panic (fmt .Sprintf ("cannot handle unexported field: %#v\n %s" , s .curPath , help ))
206207}
207208
209+ // identRx represents a valid identifier according to the Go specification.
210+ const identRx = `[_\p{L}][_\p{L}\p{N}]*`
211+
212+ var identsRx = regexp .MustCompile (`^` + identRx + `(\.` + identRx + `)*$` )
213+
208214// Transformer returns an Option that applies a transformation function that
209215// converts values of a certain type into that of another.
210216//
@@ -222,7 +228,9 @@ func (invalid) apply(s *state, _, _ reflect.Value) {
222228// to prevent the transformer from being recursively applied upon itself.
223229//
224230// The name is a user provided label that is used as the Transform.Name in the
225- // transformation PathStep. If empty, an arbitrary name is used.
231+ // transformation PathStep (and eventually shown in the Diff output).
232+ // The name must be a valid identifier or qualified identifier in Go syntax.
233+ // If empty, an arbitrary name is used.
226234func Transformer (name string , f interface {}) Option {
227235 v := reflect .ValueOf (f )
228236 if ! function .IsType (v .Type (), function .Transformer ) || v .IsNil () {
@@ -231,7 +239,7 @@ func Transformer(name string, f interface{}) Option {
231239 if name == "" {
232240 name = "λ" // Lambda-symbol as place-holder for anonymous transformer
233241 }
234- if ! isValid (name ) {
242+ if ! identsRx . MatchString (name ) {
235243 panic (fmt .Sprintf ("invalid name: %q" , name ))
236244 }
237245 tr := & transformer {name : name , fnc : reflect .ValueOf (f )}
0 commit comments