Skip to content

Commit

Permalink
cue: minor drive-by cleanups
Browse files Browse the repository at this point in the history
reflct.PtrTo was deprecated in favor of PointerTo, added in Go 1.18.

Join one reflect kind check with the reflect kind switch below,
reducing the code by a few lines.

Get rid of redundant variables for key, since it is already a string.

Thanks to sort.Slice, we no longer need to implement sort.Interface.

Signed-off-by: Daniel Martí <[email protected]>
Change-Id: I9feb1046b4ec2c7beb04d73ec8dd53df731731d1
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1168614
TryBot-Result: CUEcueckoo <[email protected]>
Reviewed-by: Paul Jolly <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
  • Loading branch information
mvdan committed Sep 7, 2023
1 parent 7b49758 commit a44c805
Showing 1 changed file with 8 additions and 17 deletions.
25 changes: 8 additions & 17 deletions cue/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ func (d *decoder) convertMap(x reflect.Value, v Value) {
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
default:
if !reflect.PtrTo(t.Key()).Implements(textUnmarshalerType) {
if !reflect.PointerTo(t.Key()).Implements(textUnmarshalerType) {
d.addErr(errors.Newf(v.Pos(), "unsupported key type %v", t.Key()))
return
}
Expand All @@ -336,21 +336,17 @@ func (d *decoder) convertMap(x reflect.Value, v Value) {

var kv reflect.Value
kt := t.Key()
switch {
case reflect.PtrTo(kt).Implements(textUnmarshalerType):
if reflect.PointerTo(kt).Implements(textUnmarshalerType) {
kv = reflect.New(kt)
err := kv.Interface().(encoding.TextUnmarshaler).UnmarshalText([]byte(key))
d.addErr(err)
kv = kv.Elem()

case kt.Kind() == reflect.String:
kv = reflect.ValueOf(key).Convert(kt)

default:
} else {
switch kt.Kind() {
case reflect.String:
kv = reflect.ValueOf(key).Convert(kt)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
s := string(key)
n, err := strconv.ParseInt(s, 10, 64)
n, err := strconv.ParseInt(key, 10, 64)
d.addErr(err)
if reflect.Zero(kt).OverflowInt(n) {
d.addErr(errors.Newf(v.Pos(), "key integer %d overflows %s", n, kt))
Expand All @@ -359,8 +355,7 @@ func (d *decoder) convertMap(x reflect.Value, v Value) {
kv = reflect.ValueOf(n).Convert(kt)

case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
s := string(key)
n, err := strconv.ParseUint(s, 10, 64)
n, err := strconv.ParseUint(key, 10, 64)
d.addErr(err)
if reflect.Zero(kt).OverflowUint(n) {
d.addErr(errors.Newf(v.Pos(), "key integer %d overflows %s", n, kt))
Expand Down Expand Up @@ -489,10 +484,6 @@ type goField struct {
// byIndex sorts goField by index sequence.
type byIndex []goField

func (x byIndex) Len() int { return len(x) }

func (x byIndex) Swap(i, j int) { x[i], x[j] = x[j], x[i] }

func (x byIndex) Less(i, j int) bool {
for k, xik := range x[i].index {
if k >= len(x[j].index) {
Expand Down Expand Up @@ -662,7 +653,7 @@ func typeFields(t reflect.Type) structFields {
}

fields = out
sort.Sort(byIndex(fields))
sort.Slice(fields, byIndex(fields).Less)

nameIndex := make(map[string]int, len(fields))
for i, field := range fields {
Expand Down

0 comments on commit a44c805

Please sign in to comment.