/
struct.clj
85 lines (62 loc) · 2.09 KB
/
struct.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
;; Copyright (c) 2015 World Singles llc
;;
;; Released under the Eclipse Public License 1.0
;; http://www.eclipse.org/legal/epl-v10.html
(ns cfml.struct
"A variant on clojure.lang.PersistentHashMap that is case insensitive
on its keys. This implements just enough methods to be able to upper
case keys on inbound operations that CFML would invoke."
(:require [clojure.string :as str])
(:gen-class :name cfml.interop.CaseInsensitiveMap
:extends clojure.lang.APersistentMap
:init clojure-struct
:state m
:constructors {[] []
[clojure.lang.IPersistentMap] []}
:methods [[keyExists [String] Boolean]])
(:import cfml.interop.CaseInsensitiveMap))
(defn -clojure-struct
"Allow an empty struct and clone to be easily created.
Do not attempt to create a struct from another data structure directly:
Use cfml.interop/struct-new or cfml.interop/to-clj-struct instead."
([]
[[] {}])
([s]
[[] s]))
;; rules for mapping keys
(defn- to-key [key] (-> key name str/upper-case (str/replace "-" "_")))
;; IPersistentMap
(defn -assoc [this key val]
(CaseInsensitiveMap. (assoc (.-m this) (to-key key) val)))
(defn -assocEx [this key val]
(if (.containsKey (.-m this) (to-key key) val)
(throw (ex-info "Key already present" {}))
(CaseInsensitiveMap. (assoc (.-m this) (to-key key) val))))
(defn -without [this key]
(CaseInsensitiveMap. (dissoc (.-m this) (to-key key))))
;; Seqable
(defn -seq [this]
(.seq (.-m this)))
;; Iterable
(defn -iterator [this]
(.iterator (.-m this)))
;; Associative
(defn -containsKey [this key]
(.containsKey (.-m this) (to-key key)))
(defn -entryAt [this key]
(.entryAt (.-m this) (to-key key)))
;; ILookup
(defn -valAt
([this key]
(.valAt (.-m this) (to-key key) nil))
([this key notFound]
(.valAt (.-m this) (to-key key) notFound)))
;; Counted
(defn -count [this]
(.count (.-m this)))
;; IPersistentCollection
(defn -empty [this]
(CaseInsensitiveMap.))
;; CFML member functions
(defn -keyExists [this key]
(.containsKey (.-m this) (to-key key)))