Skip to content

Conversation

@zachelrath
Copy link

Addresses #384 , adding the ability to specify a custom map key sort function via a registered extension. Defaults to the previous behavior, of just sorting map keys alphanumerically using < operator

Example use:

 type prioritizedSorter struct { prioritizedKey string } func (sorter *prioritizedSorter) Sort(keyA string, keyB string) bool {	// Prioritize our configured key, otherwise alpha-sort	if keyA == prioritizedKey {	return true	} else if keyB == prioritizedKey {	return false	} else {	return keyA < keyB	} } type prioritizedSortExtension struct { jsoniter.DummyExtension sorter prioritizedSorter } func (extension *prioritizedSortExtension) CreateMapKeySorter() jsoniter.MapKeySorter {	return extension.sorter } ... cfg := jsoniter.Config{ SortMapKeys: true, }.Froze() cfg.RegisterExtension(&prioritizedSortExtension{ sorter: &prioritizedSorter{ prioritizedKey: "trump", }, }) m := map[string]int{ "b": "sdfasdf" "trump": "i should be first", "a": "asdfasf", } output, _ := cfg.MarshalToString(m) 
@codecov
Copy link

codecov bot commented Jul 9, 2019

Codecov Report

Merging #385 into master will increase coverage by 0.12%.
The diff coverage is 83.33%.

Impacted file tree graph

@@ Coverage Diff @@ ## master #385 +/- ## ========================================== + Coverage 81.64% 81.76% +0.12%  ========================================== Files 41 41 Lines 5022 5041 +19 ========================================== + Hits 4100 4122 +22  + Misses 801 798 -3  Partials 121 121
Impacted Files Coverage Δ
reflect.go 92.7% <ø> (ø) ⬆️
reflect_map.go 88.26% <100%> (+0.76%) ⬆️
reflect_extension.go 83.4% <33.33%> (+0.41%) ⬆️
reflect_struct_decoder.go 48.32% <0%> (+0.41%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 27518f6...3e68b32. Read the comment docs.

@zachelrath zachelrath force-pushed the custom-map-key-sort branch from 391328f to 3e68b32 Compare July 10, 2019 13:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant