summaryrefslogtreecommitdiff
path: root/language/cloudformation/spec.scm
blob: 27e662cc77270c1fa819d5b8912004ae64cdf798 (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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
;;; guile-cloudformation --- Scheme DSL for CloudFormation templates
;;; Copyright © 2018 David Thompson <davet@gnu.org>
;;;
;;; Guile-CloudFormation is free software: you can redistribute it
;;; and/or modify it under the terms of the GNU General Public License
;;; as published by the Free Software Foundation, either version 3 of
;;; the License, or (at your option) any later version.
;;;
;;; Guile-CloudFormation is distributed in the hope that it will be
;;; useful, but WITHOUT ANY WARRANTY; without even the implied
;;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
;;; See the GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program.  If not, see
;;; <http://www.gnu.org/licenses/>.

(define-module (language cloudformation spec)
  #:use-module (aws cloudformation utils)
  #:use-module (aws cloudformation utils json)
  #:use-module (ice-9 match)
  #:use-module (ice-9 pretty-print)
  #:use-module (srfi srfi-1)
  #:use-module (system base language)
  #:export (cloudformation))

(define (property-type-name? name)
  (number? (string-index name #\.)))

(define (classify sym)
  "Add < and > around SYM to form a conventional class name."
  (symbol-append '< sym '>))

(define (compile-property-slots properties)
  (define (update-type-string->symbol s)
    (match s
      ("Conditional" 'conditional)
      ("Immutable" 'immutable)
      ("Mutable" 'mutable)))
  (define (primitive-type-checker type)
    (match type
      ("String" 'string?)
      ("Boolean" 'boolean?)
      ((or "Integer" "Long") 'integer?)
      ("Double" 'real?)
      ("Timestamp" 'date?)
      ;; TODO: deal with this
      ("Json" '(const #t))))
  (define (type-checker pspec)
    (cond
     ;; Primitive types
     ((assoc-ref pspec "PrimitiveType") =>
      primitive-type-checker)
     ;; Primitive lists/maps
     ((assoc-ref pspec "PrimitiveItemType") =>
      (lambda (item-type)
        (match (assoc-ref pspec "Type")
          ("List"
           `(lambda (items)
              (every (lambda (item)
                       (type-check ,(primitive-type-checker item-type)
                                   item))
                     items)))
          ("Map"
           `(lambda (items)
              (every (match-lambda
                       ((key . value)
                        (and (or (string? key) (symbol? key))
                             (,(primitive-type-checker item-type) value)))
                       (_ #f))
                     items))))))
     ;; Composite lists/maps
     ((assoc-ref pspec "ItemType")
      (match (assoc-ref pspec "Type")
        ("List"
         '(lambda (items) (every valid? items)))
        ("Map"
         '(lambda (items)
            (every (match-lambda
                     ((key . value)
                      (and (or (string? key) (symbol? key))
                           (valid? value)))
                     (_ #f))
                   items)))))
     ;; Composite types
     ((assoc-ref pspec "Type")
      'valid?)
     ;; Uh oh!
     (else
      (error "cannot compute type checker for property specification:" pspec))))
  (map (match-lambda
         ((pname . pspec)
          (let* ((boolean? (equal? (assoc-ref pspec "PrimitiveType")
                                   "Boolean"))
                 (slot-name-base (aws-string->symbol pname))
                 ;; Make boolean property names more
                 ;; Scheme-like by using a question mark
                 ;; at the end.
                 (slot-name (if boolean?
                                (symbol-append slot-name-base '?)
                                slot-name-base)))
            `(,slot-name
              #:init-keyword ,(symbol->keyword slot-name)
              #:accessor ,slot-name
              #:cfn-property
              (make <cloudformation-property>
                #:documentation-url ,(assoc-ref pspec "Documentation")
                #:name ,pname
                #:required? ,(assoc-ref pspec "Required")
                #:type-checker ,(type-checker pspec)
                #:update-type ',(update-type-string->symbol
                                 (assoc-ref pspec "UpdateType")))))))
       properties))

(define (strip-namespace s)
  (match (delete "" (string-split s #\:))
    ((_ _ x) x)
    ((x) x)))

(define (compile-property-type exp)
  (match exp
    ((name . spec)
     (let* ((sym (aws-string->symbol (strip-namespace name)))
            (class-name (classify sym)))
       `(begin
          (define-class ,class-name (<cloudformation-object>)
            ,@(compile-property-slots (assoc-ref spec "Properties"))
            #:cfn-name ,name
            #:documentation-url ,(assoc-ref spec "Documentation"))
          (export ,class-name
                  ;; Export all slot getter methods.
                  ,@(map (match-lambda
                           ((pname . _)
                            (aws-string->symbol pname)))
                         (assoc-ref spec "Properties"))))))))

(define (compile-resource-type exp)
  (define (primitive-type-string->symbol type)
    (match type
      ;; TODO: Handle JSON in a special way?
      ((or "Json" "String") 'string)
      ("Boolean" 'boolean)
      ("Integer" 'integer)))
  (define (compile-attributes attributes)
    (if attributes
        (map (match-lambda
               ((name . spec)
                `(cons ',(aws-string->symbol name)
                       (make <cloudformation-attribute>
                         #:name ,name
                         #:primitive-type
                         ',(primitive-type-string->symbol
                            ;; TODO: Handle attribute arrays
                            (or (assoc-ref spec "PrimitiveType")
                                (assoc-ref spec "PrimitiveItemType")))))))
             attributes)
        '()))
  (match exp
    ((name . spec)
     (let* ((sym (aws-string->symbol
                  (strip-namespace name)))
            (class-name (classify sym)))
       `(begin
          (define-class ,class-name (<cloudformation-resource>)
            ,@(compile-property-slots (assoc-ref spec "Properties"))
            #:cfn-name ,name
            #:documentation-url ,(assoc-ref spec "Documentation")
            #:attributes
            (list ,@(compile-attributes (assoc-ref spec "Attributes"))))
          (export ,class-name
                  ;; Export all slot getter methods.
                  ,@(map (match-lambda
                           ((aws-name . _)
                            (aws-string->symbol aws-name)))
                         (assoc-ref spec "Properties"))))))))

(define (compile-scheme exp env opts)
  (let ((module-suffix (string->symbol (assoc-ref exp "ModuleSuffix")))
        (version (assoc-ref exp "ResourceSpecificationVersion")))
    (values `(begin
               (define-module (aws cloudformation ,module-suffix)
                 #:use-module (aws cloudformation utils)
                 #:use-module (ice-9 match)
                 #:use-module (oop goops)
                 #:use-module (srfi srfi-1)
                 #:use-module (srfi srfi-19))
               ,@(if version
                     `((define-public cloudformation-specification-version
                         ,version))
                     '())
               ,@(map compile-property-type (assoc-ref exp "PropertyTypes"))
               ,@(map compile-resource-type (assoc-ref exp "ResourceTypes")))
            env env)))

(define-language cloudformation
  #:title "AWS Cloudformation"
  #:reader (lambda (port env)
             (if (eof-object? (peek-char port))
                 (read-char port)
                 (read port)))
  #:compilers `((scheme . ,compile-scheme))
  #:printer write)