Skip to content

Commit

Permalink
use reflectutils.TypeName() for versioned type names (#58)
Browse files Browse the repository at this point in the history
* use reflectutils.TypeName() for versioned type names

* fix race
  • Loading branch information
muir authored Feb 2, 2023
1 parent bfb9833 commit 9573d0d
Show file tree
Hide file tree
Showing 7 changed files with 7 additions and 55 deletions.
2 changes: 1 addition & 1 deletion error.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func duplicateTypes() string {
return duplicates
}
names := make(map[string]struct{})
for i := 1; i <= typeCounter; i++ {
for i := 1; i <= max; i++ {
n := typeCode(i).String()
if _, ok := names[n]; ok {
if _, ok := duplicatesFound[n]; !ok {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.16

require (
github.com/kr/pretty v0.2.0 // indirect
github.com/muir/reflectutils v0.6.0
github.com/muir/reflectutils v0.7.0
github.com/stretchr/testify v1.8.1
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfn
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/muir/reflectutils v0.6.0 h1:jXfbW60Nx+STn78vwUWVCUwInXkqAC5kWS5oeY/xPgM=
github.com/muir/reflectutils v0.6.0/go.mod h1:l8W7iTj6zMdmsWcPfsdnaAYLEuipJ7baVROqpfuonIc=
github.com/muir/reflectutils v0.7.0 h1:7ez7OLYTThDQ5kpEpxtOgFvJgtE4E11D6PVTVw+Lwl0=
github.com/muir/reflectutils v0.7.0/go.mod h1:l8W7iTj6zMdmsWcPfsdnaAYLEuipJ7baVROqpfuonIc=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
3 changes: 0 additions & 3 deletions internal/foo/foo.go

This file was deleted.

3 changes: 0 additions & 3 deletions internal/foo/v2/foo.go

This file was deleted.

20 changes: 3 additions & 17 deletions type_codes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package nject
// TODO: switch from typeCode to reflect.Type -- duplicate detection would be lost

import (
"path"
"reflect"
"regexp"
"strings"
"sync"

"github.com/muir/reflectutils"
)

type typeCode int
Expand Down Expand Up @@ -76,22 +75,9 @@ func (tc typeCode) Type() reflect.Type {
return reverseMap[tc]
}

var versionRE = regexp.MustCompile(`/v(\d+)$`)

// Type returns the reflect.Type for this typeCode
func (tc typeCode) String() string {
ts := tc.Type().String()
if versionRE.MatchString(tc.Type().PkgPath()) {
pp := tc.Type().PkgPath()
version := path.Base(pp)
pn := path.Base(path.Dir(pp))
revised := strings.Replace(ts, pn, pn+"/"+version, 1)
if revised != ts {
return revised
}
return "(" + version + ")" + ts
}
return ts
return reflectutils.TypeName(tc.Type())
}

func (tcs typeCodes) Types() []reflect.Type {
Expand Down
28 changes: 0 additions & 28 deletions type_codes_test.go

This file was deleted.

0 comments on commit 9573d0d

Please sign in to comment.