Skip to content

Commit 68606cf

Browse files
lenzlenz
authored andcommitted
Creazione corretta delle Interfacce per classi Action
1 parent 3a4d150 commit 68606cf

File tree

7 files changed

+275
-14
lines changed

7 files changed

+275
-14
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@ build.xml
2525
# netbeans
2626
.nb-gradle/
2727

28+
#codegen3
29+
codegen3/gen-java/
30+
31+

codegen3/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@ FIXME: listing of options this app accepts.
3232

3333
Copyright © 2016 FIXME
3434

35-
Distributed under the Eclipse Public License either version 1.0 or (at
36-
your option) any later version.
35+
Distributed ...........
36+
37+

codegen3/src/codegen3/core.clj

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
(ns codegen3.core
22
(:require [clojure.data.json :as json])
3+
(:require [codegen3.methods :as meth])
4+
(:require [codegen3.javagen :as javagen])
5+
6+
37
(:gen-class))
48

59
;; Load func: cmd+enter
@@ -52,6 +56,10 @@
5256
(def ARI_VERSIONS
5357
["ari_0_0_1" "ari_1_0_0"])
5458

59+
60+
(def ARI-VERSIONS-KW
61+
(vec (map keyword ARI_VERSIONS)))
62+
5563
;; ALL KNOWN ARI FILES
5664
(def ALL_FILES
5765
{"applications" {},
@@ -62,7 +70,8 @@
6270
"events" {}
6371
})
6472

65-
73+
(def ALL-FILES-KW
74+
(vec (map keyword (keys ALL_FILES))))
6675

6776
;; READ ARI FOLDER as JSON
6877
(defn loadResourceFile
@@ -113,6 +122,41 @@
113122
(reduce readKnownFilesForVersion {} ARI_VERSIONS))
114123

115124

125+
(defn allFilesAllAris
126+
"Ritorna tuple di tutte le permutazioni di file e di tutte le ARI,
127+
anche quelle che non esistono"
128+
[]
129+
(for [ari ARI_VERSIONS
130+
file (keys ALL_FILES)]
131+
[ (keyword ari) (keyword file)] ))
132+
133+
134+
(defn mkFnSignature
135+
"Creates a list of signatures for each file"
136+
[apiOp]
137+
138+
)
139+
140+
141+
142+
(defn write$interfaces [DB]
143+
(for [file ALL-FILES-KW]
144+
(javagen/writeInterface
145+
file
146+
"--comment--"
147+
(vals (meth/allSigsForInterface DB file ARI-VERSIONS-KW)))))
148+
149+
(defn run$
150+
[]
151+
(let [DB (readAll)]
152+
(do
153+
(write$interfaces DB)
154+
)
155+
))
156+
157+
158+
159+
116160

117161
;; How is this supposed to be working?
118162
;; 1. we read the contents of known files in all
@@ -122,3 +166,19 @@
122166
;; 3. we build events
123167
;; 4. we build ???
124168
;; 5. for each version of the interface, we build ...
169+
170+
171+
;; READ ALL METHODS
172+
;; (def DT (readAll))
173+
;; apis is an array
174+
;; (get-in DT [:ari_1_0_0 :asterisk :apis 0 :operations])
175+
176+
;; READS ALL MODELS
177+
;; (keys (get-in DT [:ari_1_0_0 :asterisk :models]))
178+
;; a specific model
179+
;; (get-in DT [:ari_1_0_0 :asterisk :models :BuildInfo])
180+
181+
182+
183+
184+
;; SVILUPPO

codegen3/src/codegen3/javagen.clj

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,23 @@
1111
" Generated on: " (.toString (java.util.Date.))
1212
"\n\n"))
1313

14+
15+
16+
;ch.loway.oss.ari4java.generated
17+
(def BASE-GEN-PKG "gg")
18+
19+
20+
(def IMPORTS-INTERFACE [
21+
"java.util.Date"
22+
"java.util.List"
23+
"java.util.Map"
24+
"java.util.ArrayList"
25+
"ch.loway.oss.ari4java.tools.RestException"
26+
"ch.loway.oss.ari4java.tools.AriCallback"
27+
"ch.loway.oss.ari4java.tools.tags.*" ])
28+
29+
30+
1431
;(genclassfile {:package "a.b.c"
1532
; :classname "Pippon"
1633
; :imports ["java.w"]
@@ -30,6 +47,7 @@
3047
"Creates a camelName - es getDate by joining 'get' (prefix)
3148
and 'date' (value)"
3249
[p v]
50+
{:pre [(pos? (count v))]}
3351
(str p
3452
(s/upper-case (subs v 0 1))
3553
(subs v 1)))
@@ -78,12 +96,18 @@
7896
)))
7997

8098

99+
(defn genAttr
100+
"Generate an attribute. Checks that :type and :name are defined."
101+
[{t :type n :name}]
102+
{:pre [(string? t) (string? n)]}
103+
(str t " " n))
81104

82105

83106
(defn genAttrs
84-
"The attributes for a method."
107+
"The attributes for a method.
108+
data is a list of attributes."
85109
[data]
86-
(let [signatures (map #(str (:type %) " " (:name %)) data)]
110+
(let [signatures (map genAttr data)]
87111
(s/join ", " signatures)))
88112

89113

@@ -95,20 +119,26 @@
95119
:args [{:type :name}]
96120
:notes
97121
:body
122+
:isAbstract
98123
"
99124
[data]
100-
(let [{:keys [method returns isPrivate args notes body]} data]
125+
(let [{:keys [method returns isPrivate args notes body isAbstract]} data]
101126
(str
102127
"\n\n"
103128
(mkComment notes)
104129
(if isPrivate "private " "public ")
105130
returns " "
106131
method "("
107132
(genAttrs args)
108-
"){\n"
109-
(indent body)
133+
")"
134+
(if isAbstract
135+
";"
136+
(str
137+
"{\n"
138+
(indent body)
139+
"\n"
140+
"}"))
110141
"\n"
111-
"}\n"
112142

113143
)))
114144

@@ -270,13 +300,31 @@
270300
(spit realPath body)
271301
realPath))
272302

273-
;(writeOutKlass
274-
;(mkDataClass "p.k.g" "c" "n"
275-
; [{:type "int" :val "pluto"}
276-
; {:type "String" :val "pippo"}]
277-
; ))
278303

279304

305+
(defn writeInterface
306+
"Una interfaccia è scritta su disco sulla base del nome del file e dei metodi"
307+
[file comments meths]
308+
(let [klass {
309+
:classname (camelName "Action" (name file))
310+
:isInterface true
311+
:package BASE-GEN-PKG
312+
:imports IMPORTS-INTERFACE
313+
;:implements ""
314+
;:extends the class to extend
315+
:notes comments
316+
:functions meths
317+
;:stanzas a list of text to implement (added after the methods)
318+
}]
319+
(writeOutKlass klass) ) )
320+
321+
322+
323+
324+
;(writeOutKlass
325+
; (mkDataClass "p.k.g" "c" "no"
326+
; [{:type "int" :name "pluto"} {:type "String" :name "pippno"}] ))
327+
280328

281329

282330

codegen3/src/codegen3/methods.clj

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
(ns codegen3.methods
2+
(:require [clojure.data.json :as json]
3+
[codegen3.javagen :as javagen]
4+
[codegen3.signatures :as sgn])
5+
(:gen-class) )
6+
7+
8+
;; Il metodo di base si aspetta:
9+
;; :path
10+
;; :descr
11+
;; :op (che è una sola delle operations)
12+
13+
14+
; {
15+
; "path": "/applications/{applicationName}",
16+
; "description": "Stasis application",
17+
; "operations": [
18+
; {
19+
; "httpMethod": "GET",
20+
; "summary": "Get details of an application.",
21+
; "nickname": "get",
22+
; "responseClass": "Application",
23+
; "parameters": [
24+
; {
25+
; "name": "applicationName",
26+
; "description": "Application's name",
27+
; "paramType": "path",
28+
; "required": true,
29+
; "allowMultiple": false,
30+
; "dataType": "string"
31+
; }
32+
; ],
33+
; "errorResponses": [
34+
; {
35+
; "code": 404,
36+
; "reason": "Application does not exist."
37+
; }
38+
; ]
39+
; }
40+
; ]
41+
; }
42+
43+
44+
(defn mkParm
45+
"Dato un elemento parameters e un tipo di espansione, ritorna il
46+
parametro in formato {:type :name :comment}"
47+
[parm exp]
48+
(let [n (:name parm)
49+
t (javagen/typeTranslator (:dataType parm) "")
50+
c (:description parm)]
51+
{:type t
52+
:name n
53+
:comment c}))
54+
55+
56+
(defn baseSignature
57+
"La signature di base, senza callback"
58+
[path descr op]
59+
(let [nick (:nickname op)
60+
summary (str descr "\n" (:summary op))
61+
response (javagen/typeTranslator (:responseClass op) "")
62+
parms (:parameters op)]
63+
{
64+
:method nick
65+
:returns response
66+
:isPrivate false
67+
:args (mapv #(mkParm % "") parms)
68+
:notes summary
69+
:body ""
70+
:isAbstract true
71+
}))
72+
73+
74+
(defn allSignsForPath
75+
"Per un path dato, tutte le sue signatures"
76+
[path-str]
77+
(let [path (:path path-str)
78+
des (:description path-str)
79+
ops (:operations path-str)]
80+
(map #(baseSignature path des %) ops)))
81+
82+
83+
84+
(defn allSigsForAriFile
85+
"Tutte le signatures per un file dato di un'ari data"
86+
[db ari-ver file]
87+
(let [file-paths (get-in db [ari-ver file :apis])]
88+
(flatten (mapv allSignsForPath file-paths))))
89+
90+
91+
(defn- rdc-addSigToMap
92+
[acc sig]
93+
(let [k (sgn/getMethodSignature sig)]
94+
(assoc-in acc [k] sig)))
95+
96+
97+
(defn allSigsForInterface
98+
"Tutte le signatures per creare un'interfaccia.
99+
Per calcolarle:
100+
- cerco le signatures per tutte le versioni note dell'ari
101+
- le rendo univoche sulla base della loro signature java
102+
Restituisco un hash la cui chiave è la sig Java ed il contenuto è
103+
il metodo.
104+
"
105+
[DB file ari-versions]
106+
(let [all-possible-sgns
107+
(reduce into []
108+
(map #(allSigsForAriFile DB % file) ari-versions))]
109+
(reduce rdc-addSigToMap {} all-possible-sgns)
110+
111+
))
112+
113+
114+
115+
;(def DB (readAll))
116+
;(reduce into #{} (map #(allSigsForAriFile DB % :applications)
117+
; [:ari_0_0_1 :ari_1_0_0])))
118+
119+
120+
121+
122+
123+
124+
125+
126+

codegen3/src/codegen3/signatures.clj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,9 @@
2727
(map getMethodSignature lFuncs)
2828
; AGGIUNGERE ASSERTIONS: metodi unici
2929
)
30+
31+
32+
(defn constrained-sqr [x]
33+
{:pre [(pos? x)]
34+
:post [(> % 16), (< % 225)]}
35+
(* x x))
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
(ns codegen3.methods-test
2+
(:require [clojure.test :refer :all]
3+
[codegen3.methods :refer :all]))
4+
5+
(def PARM {:name "applicationName"
6+
:description "descr"
7+
:paramType "path"
8+
:required true
9+
:allowMultiple false
10+
:dataType "string" })
11+
12+
13+
(deftest plainParm
14+
(testing "Parametro semplice, tipo nativo"
15+
(is (= {:type "String", :name "applicationName", :comment "descr"}
16+
(mkParm PARM :PLAIN)))))

0 commit comments

Comments
 (0)