aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/world/environment.old.lux
blob: 8ad10f1f9fbfa4c0d43f38783d5e3509942974ce (plain)
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
(.module:
  [lux #*
   [data
    ["." text]
    [format
     [context (#+ Context)]]
    [collection
     ["." dictionary]]]
   [io (#- run)]
   [host (#+ import:)]])

## Do not trust the values of environment variables
## https://wiki.sei.cmu.edu/confluence/display/java/ENV02-J.+Do+not+trust+the+values+of+environment+variables

(import: java/lang/String)

(import: (java/util/Map$Entry k v)
  (getKey [] k)
  (getValue [] v))

(import: (java/util/Iterator a)
  (hasNext [] boolean)
  (next [] a))

(import: (java/util/Set a)
  (iterator [] (Iterator a)))

(import: (java/util/Map k v)
  (entrySet [] (Set (Map$Entry k v))))

(import: java/lang/System
  (#static getenv [] (Map String String)))

(def: (consume-iterator f iterator)
  (All [a b] (-> (-> a b) (Iterator a) (List b)))
  (if (Iterator::hasNext iterator)
    (#.Cons (f (Iterator::next iterator))
            (consume-iterator f iterator))
    #.Nil))

(def: (entry-to-kv entry)
  (All [k v] (-> (Map$Entry k v) [k v]))
  [(Map$Entry::getKey entry)
   (Map$Entry::getValue entry)])

(def: #export read
  (IO Context)
  (io (|> (System::getenv)
          Map::entrySet
          Set::iterator
          (consume-iterator entry-to-kv)
          (dictionary.from-list text.hash))))