-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #473 from fxamacker/fxamacker/refactor-map-encode-…
…to-prepare-for-go-verion-bump Refactor map encoding to prep for Go version bump
- Loading branch information
Showing
3 changed files
with
70 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) Faye Amacker. All rights reserved. | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
package cbor | ||
|
||
import ( | ||
"reflect" | ||
) | ||
|
||
type mapKeyValueEncodeFunc struct { | ||
kf, ef encodeFunc | ||
} | ||
|
||
func (me *mapKeyValueEncodeFunc) encodeKeyValues(e *encoderBuffer, em *encMode, v reflect.Value, kvs []keyValue) error { | ||
trackKeyValueLength := len(kvs) == v.Len() | ||
|
||
iter := v.MapRange() | ||
for i := 0; iter.Next(); i++ { | ||
off := e.Len() | ||
|
||
if err := me.kf(e, em, iter.Key()); err != nil { | ||
return err | ||
} | ||
if trackKeyValueLength { | ||
kvs[i].keyLen = e.Len() - off | ||
} | ||
|
||
if err := me.ef(e, em, iter.Value()); err != nil { | ||
return err | ||
} | ||
if trackKeyValueLength { | ||
kvs[i].keyValueLen = e.Len() - off | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func getEncodeMapFunc(t reflect.Type) encodeFunc { | ||
kf, _ := getEncodeFunc(t.Key()) | ||
ef, _ := getEncodeFunc(t.Elem()) | ||
if kf == nil || ef == nil { | ||
return nil | ||
} | ||
mkv := &mapKeyValueEncodeFunc{kf: kf, ef: ef} | ||
return mapEncodeFunc{ | ||
e: mkv.encodeKeyValues, | ||
}.encode | ||
} |