@@ -21,13 +21,15 @@ type Pair[K comparable, V any] struct {
2121}
2222
2323type OrderedMap [K comparable , V any ] struct {
24- pairs map [K ]* Pair [K , V ]
25- list * list.List [* Pair [K , V ]]
24+ pairs map [K ]* Pair [K , V ]
25+ list * list.List [* Pair [K , V ]]
26+ disableHTMLEscape bool
2627}
2728
2829type initConfig [K comparable , V any ] struct {
29- capacity int
30- initialData []Pair [K , V ]
30+ capacity int
31+ initialData []Pair [K , V ]
32+ disableHTMLEscape bool
3133}
3234
3335type InitOption [K comparable , V any ] func (config * initConfig [K , V ])
@@ -49,6 +51,13 @@ func WithInitialData[K comparable, V any](initialData ...Pair[K, V]) InitOption[
4951 }
5052}
5153
54+ // WithDisableHTMLEscape disables HTMl escaping when marshalling to JSON
55+ func WithDisableHTMLEscape [K comparable , V any ]() InitOption [K , V ] {
56+ return func (c * initConfig [K , V ]) {
57+ c .disableHTMLEscape = true
58+ }
59+ }
60+
5261// New creates a new OrderedMap.
5362// options can either be one or several InitOption[K, V], or a single integer,
5463// which is then interpreted as a capacity hint, à la make(map[K]V, capacity).
@@ -63,6 +72,11 @@ func New[K comparable, V any](options ...any) *OrderedMap[K, V] {
6372 invalidOption ()
6473 }
6574 config .capacity = option
75+ case bool :
76+ if len (options ) != 1 {
77+ invalidOption ()
78+ }
79+ config .disableHTMLEscape = option
6680
6781 case InitOption [K , V ]:
6882 option (& config )
@@ -72,7 +86,7 @@ func New[K comparable, V any](options ...any) *OrderedMap[K, V] {
7286 }
7387 }
7488
75- orderedMap .initialize (config .capacity )
89+ orderedMap .initialize (config .capacity , config . disableHTMLEscape )
7690 orderedMap .AddPairs (config .initialData ... )
7791
7892 return orderedMap
@@ -82,9 +96,10 @@ const invalidOptionMessage = `when using orderedmap.New[K,V]() with options, eit
8296
8397func invalidOption () { panic (invalidOptionMessage ) }
8498
85- func (om * OrderedMap [K , V ]) initialize (capacity int ) {
99+ func (om * OrderedMap [K , V ]) initialize (capacity int , disableHTMLEscape bool ) {
86100 om .pairs = make (map [K ]* Pair [K , V ], capacity )
87101 om .list = list .New [* Pair [K , V ]]()
102+ om .disableHTMLEscape = disableHTMLEscape
88103}
89104
90105// Get looks for the given key, and returns the value associated with it,
0 commit comments