Skip to content

Commit aed7d41

Browse files
committed
Release v1
2 parents 76f435e + a0871a4 commit aed7d41

11 files changed

+619
-1
lines changed

README.md

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,110 @@
1-
# declarative-cos
1+
# Welcome to the DeclarativeCOS!
2+
<br/>
3+
<br/>
4+
<br/>
5+
# What is DeclarativeCOS?
6+
7+
DeclarativeCOS - is another view to programming on the Caché ObjectScript language. It allows you to write the code in declarative style.
8+
<br/>
9+
<br/>
10+
# What is declarative style in COS?
11+
12+
Declarative style in COS means that you write the code as a statement which describes what you need to do.
13+
<br/>
14+
<br/>
15+
# What is the difference from my one-liners code?
16+
17+
The main point is not to add one-liners features to COS. The main point is to bring another kind of mind when you do your task. DeclarativeCOS allows to remove regular loops from your code, because you really don't need it.
18+
<br/>
19+
<br/>
20+
# What is the problem with my regular loops in COS?
21+
22+
Loop is just an instrument to solve the problem. In common, the problem is to traverse by collection and do some action with every element. Do you really need a loop for it? No. You choose a loop because COS supports loops only. DeclarativeCOS allows to write the code in declarative style. Just declare what you want to do and DeclarativeCOS will do all the rest for you.
23+
<br/>
24+
<br/>
25+
# OK. How does it work?
26+
27+
**5 steps:**
28+
- Extends from DeclarativeCOS.DeclarativeProvider.
29+
- Implement class method.
30+
- Mark the method.
31+
- Use one of provided DeclarativeCOS statements.
32+
- Enjoy the result.
33+
34+
<br/>
35+
<br/>
36+
# Any example, please.
37+
38+
Suppose, we need to output all items of the list (for example, list is instance of [%ListOfDataTypes](http://docs.intersystems.com/latest/csp/documatic/%25CSP.Documatic.cls?PAGE=CLASS&LIBRARY=%25SYS&CLASSNAME=%25Library.ListOfDataTypes)).
39+
40+
**Step 1. Extends from DeclarativeCOS.DeclarativeProvider.**
41+
```
42+
Class MyPackage.IO extens DeclarativeProvider
43+
{
44+
}
45+
```
46+
47+
**Step 2. Implement class method.**
48+
```
49+
Class MyPackage.IO extens DeclarativeProvider
50+
{
51+
52+
ClassMethod print(value As %String)
53+
{
54+
w value
55+
}
56+
57+
}
58+
```
59+
60+
**Step 3. Mark the method.**
61+
```
62+
Class MyPackage.IO extens DeclarativeProvider
63+
{
64+
65+
/// @Declarative("io:print")
66+
ClassMethod print(value As %String)
67+
{
68+
w value
69+
}
70+
71+
}
72+
```
73+
74+
**Step 4. Use one of provided DeclarativeCOS statements.**
75+
```
76+
s words = ##class(%Library.ListOfDataTypes).%New()
77+
d words.Insert("Hello ")
78+
d words.Insert("World!")
79+
80+
zforeach $zbind(words, "io:print")
81+
```
82+
83+
**Step 5. Enjoy the result.**
84+
```
85+
Hello World!
86+
```
87+
<br/>
88+
# What is about LICENCE?
89+
90+
MIT License
91+
92+
Copyright (c) 2017 InterSystems Corporation
93+
94+
Permission is hereby granted, free of charge, to any person obtaining a copy
95+
of this software and associated documentation files (the "Software"), to deal
96+
in the Software without restriction, including without limitation the rights
97+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
98+
copies of the Software, and to permit persons to whom the Software is
99+
furnished to do so, subject to the following conditions:
100+
101+
The above copyright notice and this permission notice shall be included in all
102+
copies or substantial portions of the Software.
103+
104+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
105+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
106+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
107+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
108+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
109+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
110+
SOFTWARE.

src/cls/DeclarativeCOS.Binder.cls

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
Class DeclarativeCOS.Binder Extends %RegisteredObject
2+
{
3+
4+
Property collection As %Collection.AbstractList [ Private ];
5+
6+
Property function As %String [ Private ];
7+
8+
Method %OnNew(collection As %Collection.AbstractList, function As %String) As %Status [ Private, ServerOnly = 1 ]
9+
{
10+
if ('##class(DeclarativeCOS.Utils).isValidName(function)) {
11+
set exception = ##class(%Exception.General).%New("Declarative name is invalid", "1",,"Declarative name pattern must be ""namespace:function"".")
12+
13+
throw exception
14+
}
15+
16+
set ..collection = collection
17+
18+
set ..function = ##class(DeclarativeCOS.Utils).normalizeName(function)
19+
20+
return $$$OK
21+
}
22+
23+
Method GetCollection()
24+
{
25+
return ..collection
26+
}
27+
28+
Method GetFunction()
29+
{
30+
return ..function
31+
}
32+
33+
Method ForEach()
34+
{
35+
quit:..collection=""
36+
37+
set index = ""
38+
39+
for {
40+
set index = ..collection.Next(index)
41+
42+
quit:index=""
43+
44+
set item = ..collection.GetAt(index)
45+
46+
set $lb(className, methodName) = ..loadDeclarative(..function)
47+
48+
do $classmethod(className, methodName, item)
49+
}
50+
}
51+
52+
Method Map()
53+
{
54+
set collection = $classmethod(..collection.%ClassName(), "%New")
55+
56+
set index = ""
57+
58+
for {
59+
set index = ..collection.Next(index)
60+
61+
quit:index=""
62+
63+
set item = ..collection.GetAt(index)
64+
65+
set $lb(className, methodName) = ..loadDeclarative(..function)
66+
67+
set mappedItem = $classmethod(className, methodName, item)
68+
69+
do collection.Insert(mappedItem)
70+
}
71+
72+
return collection
73+
}
74+
75+
Method Find()
76+
{
77+
set index = ""
78+
79+
for {
80+
set index = ..collection.Next(index)
81+
82+
quit:index=""
83+
84+
set item = ..collection.GetAt(index)
85+
86+
set $lb(className, methodName) = ..loadDeclarative(..function)
87+
88+
if ($classmethod(className, methodName, item)) {
89+
return item
90+
}
91+
}
92+
93+
return ""
94+
}
95+
96+
Method Filter()
97+
{
98+
set collection = $classmethod(..collection.%ClassName(), "%New")
99+
100+
set index = ""
101+
102+
for {
103+
set index = ..collection.Next(index)
104+
105+
quit:index=""
106+
107+
set item = ..collection.GetAt(index)
108+
109+
set $lb(className, methodName) = ..loadDeclarative(..function)
110+
111+
if ($classmethod(className, methodName, item)) {
112+
do collection.Insert(item)
113+
}
114+
}
115+
116+
return collection
117+
}
118+
119+
Method Exists()
120+
{
121+
return ..Find() '= ""
122+
}
123+
124+
Method Count()
125+
{
126+
set count = 0
127+
128+
set index = ""
129+
130+
for {
131+
set index = ..collection.Next(index)
132+
133+
quit:index=""
134+
135+
set item = ..collection.GetAt(index)
136+
137+
set $lb(className, methodName) = ..loadDeclarative(..function)
138+
139+
if ($classmethod(className, methodName, item)) {
140+
set count = count + 1
141+
}
142+
}
143+
144+
return count
145+
}
146+
147+
ClassMethod loadDeclarative(function As %String)
148+
{
149+
return ##class(DeclarativeCOS.DeclarativesManager).loadDeclarative(function)
150+
}
151+
152+
}
153+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
Class DeclarativeCOS.DeclarativeProvider
2+
{
3+
4+
ClassMethod register() [ CodeMode = objectgenerator ]
5+
{
6+
if (%compiledclass.Name = "DeclarativeCOS.DeclarativeProvider") {
7+
kill ^DeclarativeCOS
8+
}
9+
10+
set methods = %compiledclass.Methods
11+
12+
set i = ""
13+
14+
for {
15+
set i = methods.Next(i)
16+
17+
quit:i=""
18+
19+
set method = methods.GetAt(i)
20+
21+
set methodName = method.Name
22+
23+
set methodDescription = method.Description
24+
25+
continue:methodName="register"
26+
27+
continue:$extract(methodName)="%"
28+
29+
set declarativeNameRegex = ##class(DeclarativeCOS.Utils).#DECLARATIVENAMEREGEX
30+
31+
set annotationRegexp = "@Declarative\(""(" _ declarativeNameRegex _ ")""\)"
32+
33+
set annotationMatcher = ##class(%Regex.Matcher).%New(annotationRegexp, methodDescription)
34+
35+
if (annotationMatcher.Locate()) {
36+
set annotationValue = $replace(annotationMatcher.Group(1), " ", "")
37+
38+
do ##class(DeclarativeCOS.DeclarativesManager).saveDeclarative(annotationValue, %compiledclass.Name, methodName)
39+
}
40+
}
41+
42+
d %code.WriteLine(" q")
43+
44+
q $$$OK
45+
}
46+
47+
}
48+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Class DeclarativeCOS.DeclarativesManager
2+
{
3+
4+
ClassMethod saveDeclarative(declarativeName As %String, className As %String, functionName As %String)
5+
{
6+
set ^DeclarativeCOS("functions", declarativeName) = $lb(className, functionName)
7+
}
8+
9+
ClassMethod loadDeclarative(declarativeName As %String)
10+
{
11+
return $get(^DeclarativeCOS("functions", declarativeName))
12+
}
13+
14+
}
15+

src/cls/DeclarativeCOS.IO.cls

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Class DeclarativeCOS.IO Extends DeclarativeCOS.DeclarativeProvider
2+
{
3+
4+
/// @Declarative("io:print")
5+
ClassMethod print(value As %Library.DataType)
6+
{
7+
w value
8+
}
9+
10+
/// @Declarative("io:println")
11+
ClassMethod println(value As %Library.DataType)
12+
{
13+
w value, !
14+
}
15+
16+
}
17+

src/cls/DeclarativeCOS.Joiner.cls

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
Class DeclarativeCOS.Joiner
2+
{
3+
4+
ClassMethod join(collection As %Collection.AbstractList, separator As %String = "") As %String
5+
{
6+
if (collection = "") {
7+
return ""
8+
}
9+
10+
set string = ""
11+
12+
set index = ""
13+
14+
for {
15+
set index = collection.Next(index)
16+
17+
quit:index=""
18+
19+
set item = collection.GetAt(index)
20+
21+
if (string = "") {
22+
set string = "" _ item
23+
}
24+
else {
25+
set string = string _ separator _ item
26+
}
27+
}
28+
29+
return string
30+
}
31+
32+
}
33+

0 commit comments

Comments
 (0)