Skip to content

Commit e892c3e

Browse files
committed
raw client is now being generated by a bash script and scanning the elasticsearch source code
1 parent 3cda53f commit e892c3e

File tree

10 files changed

+6250
-1115
lines changed

10 files changed

+6250
-1115
lines changed

generate_rest_urls.sh

Lines changed: 160 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
ESFOLDER="../elasticsearch"
88
DEFINITIONOUTPUTFILE="src/Generated/rest-actions.txt"
9+
ESCOMMIT=$(cd $ESFOLDER | git rev-parse HEAD)
910

1011
# Find all the lines that registerHandlers
1112
grep -r "controller.registerHandler" ${ESFOLDER}/src/main/java/org/elasticsearch/rest/ |
@@ -67,44 +68,168 @@ grep -r "controller.registerHandler" ${ESFOLDER}/src/main/java/org/elasticsearch
6768
# Sort and make sure all the lines are unique again
6869
sort | uniq > $DEFINITIONOUTPUTFILE
6970

70-
71-
# Generate intermediate definition file
72-
cat src/Generated/rest-actions.txt |
73-
sed -e 's/\([^ ]*\) \([^ ]*\) \(.*\)$/\/\/\/<summary>\2 \3<\/summary>\nConnectionStatus \1\2(\3);\n\n\/\/\/<summary>\2 \3<\/summary>\nTask<ConnectionStatus> \1\2Async(\3);\n/' |
74-
cat > src/Generated/intermediate-definition.txt
75-
76-
# Patch defintion file so url params become method params
77-
echo "" > src/Generated/intermediate-definition-params.txt
78-
cat src/Generated/intermediate-definition.txt | while read LINE ; do
71+
to_interface() {
72+
cat src/Generated/rest-actions.txt |
73+
sed -e 's/\([^ ]*\) \([^ ]*\) \(.*\)$/\/\/\/<summary>\2 \3<\/summary>\nConnectionStatus \1\2(\3);\n\/\/\/<summary>\2 \3<\/summary>\nConnectionStatus \1\2Async(\3);/' |
74+
sed -e '2~2 s/\/{/, string /g' | #replace /{ with string param
75+
sed -e '2~2 s/}\([^,]*\)//g' | #remove } up to first , or )
76+
sed -e '2~2 s/(, string/(string/'| #touch up first param
77+
sed -e '2~2 s/\/[^,]*//g' | #remove urls bit from params.
78+
#Render body parameter for all the verbs except GET and HEAD
79+
sed -e '2~2 s/\(.*\)\(POST\|PUT\|DELETE\)\(.*\)$/\1\2\3, object body, NameValueCollection queryString = null);/' |
80+
81+
# GET's and HEAD cannot have a BODY (.NET limitation)
82+
sed -e '2~2 s/\(.*\)\(GET\|HEAD\)\(.*\)$/\1\2\3, NameValueCollection queryString = null);/' |
83+
84+
sed -e '2~2 s/(, /(/g' | #remove any empty first params
85+
86+
#Touching up the methodnames as best as we can
87+
sed -e '2~2 s/\(Get\w*\)GET/\1/g' | #remove starting Get if GET is already in the method name
88+
sed -e '2~2 s/\(Delete\w*\)DELETE/\1/g' | #remove DELETE if Delete is already in the method name (flows better)
89+
sed -e '2~2 s/\(Head\w*\)HEAD/\1/g' | #remove HEAD if Head is already in the method name (flows better)
90+
sed -e '2~2 s/\(Put\w*\)PUT/\1/g' | #remove PUT if Put is already in the method name (flows better)
91+
sed -e '2~2 s/GET/Get/g' | #replace GET with Get
92+
sed -e '2~2 s/HEAD/Head/g' | #replace HEAD with Head
93+
sed -e '2~2 s/PUT/Put/g' | #replace PUT with Put
94+
sed -e '2~2 s/POST/Post/g' | #replace POST with Post
95+
sed -e '2~2 s/DELETE/Delete/g' | #replace DELETE with Delete
96+
#return Task for async operations
97+
sed -e '2~2 s/ConnectionStatus\(.*\)Async/Task<ConnectionStatus>\1Async/g' |
98+
cat
99+
}
100+
101+
###
102+
# - IRawElasticClient.cs
103+
###
104+
cat >src/NEST/IRawElasticClient.cs <<EOL
105+
using System;
106+
using System.Collections.Generic;
107+
using System.Collections.Specialized;
108+
using System.Linq;
109+
using System.Text;
110+
using System.Threading.Tasks;
111+
112+
///Generated using generate-raw-client.sh in NEST's root
113+
namespace Nest
114+
{
115+
///<summary>
116+
///Raw operations with elasticsearch
117+
///<pre>
118+
///This file is automatically generated from the elasticsearch repository itself
119+
///</pre>
120+
///<pre>
121+
///Generated of commit $ESCOMMIT
122+
///</pre>
123+
///</summary>
124+
public interface IRawElasticClient
125+
{
126+
EOL
127+
128+
to_interface |
129+
sed -e 's/^\/\//\n\/\//' | #newline before xmldocs
130+
sed -e 's/^\(.\)/\t\t\1/' | #lines with data indent twice
131+
cat >> src/NEST/IRawElasticClient.cs
132+
133+
134+
cat >>src/NEST/IRawElasticClient.cs <<EOL
135+
}
136+
}
137+
EOL
138+
139+
###
140+
# - RawElasticClient-Client.cs
141+
###
142+
143+
to_interface >src/Generated/tmp.txt
144+
145+
VERB=""
146+
URL=""
147+
PARAMS=""
148+
149+
cat >src/Nest/RawElasticClient-Generated.cs <<EOL
150+
using System.Collections.Specialized;
151+
using System.Threading.Tasks;
152+
153+
///Generated using generate-raw-client.sh in NEST's root
154+
namespace Nest
155+
{
156+
///<summary>
157+
///Raw operations with elasticsearch
158+
///<pre>
159+
///This file is automatically generated from the elasticsearch repository itself
160+
///</pre>
161+
///<pre>
162+
///Generated of commit $ESCOMMIT
163+
///</pre>
164+
///</summary>
165+
public partial class RawElasticClient : IRawElasticClient
166+
{
167+
EOL
168+
169+
#TODO rewrite this to single sed chain like we do for IRawElasticClient
170+
#Looping with while read line is wayyy to slow
171+
172+
cat src/Generated/tmp.txt | while read LINE ; do
79173
N=$((N+1))
80174

81-
if [[ $LINE = *ConnectionStatus* ]]
175+
176+
if [[ $LINE = *ConnectionStatus* ]];
82177
then
83-
echo "$LINE" |
84-
sed -e 's/\/{/, string /g' | #replace /{ with string param
85-
sed -e 's/}\([^,]*\)//g' | #remove } up to first , or )
86-
sed -e 's/(, string/(string/'| #touch up first param
87-
sed -e 's/\/[^,]*//g' | #remove urls bit from params.
88-
if [[ $LINE = *GET* ]]
89-
then
90-
sed -e 's/$/, NameValueCollection queryString = null);/'
91-
else
92-
sed -e 's/$/, object body, NameValueCollection queryString = null);/'
93-
fi | #do not render a body parameter for GET calls
94-
sed -e 's/(, /(/g' | #remove any empty first params
95-
96-
#Touching up the methodnames as best as we can
97-
sed -e ' s/\(Get\w*\)GET/\1/g' | #remove starting Get if GET is already in the method name
98-
sed -e ' s/\(Delete\w*\)DELETE/\1/g' | #remove DELETE if Delete is already in the method name (flows better)
99-
sed -e ' s/\(Head\w*\)HEAD/\1/g' | #remove HEAD if Head is already in the method name (flows better)
100-
sed -e ' s/\(Put\w*\)PUT/\1/g' | #remove PUT if Put is already in the method name (flows better)
101-
sed -e ' s/GET/Get/g' | #replace GET with Get
102-
sed -e ' s/HEAD/Head/g' | #replace HEAD with Head
103-
sed -e ' s/PUT/Put/g' | #replace PUT with Put
104-
sed -e ' s/POST/Post/g' | #replace POST with Post
105-
sed -e ' s/DELETE/Delete/g' #replace DELETE with Delete
178+
# Check if the current method def is for an async method
179+
Async="";
180+
if [[ "$LINE" == *Async* ]]; then
181+
Async="Async";
182+
183+
fi
184+
printf "\tpublic $LINE\n" |
185+
#remove trailing slash from line
186+
sed -e 's/;$//'
187+
188+
189+
printf "\t{\n"
190+
191+
# If the route has no params we can just assign the route to path
192+
if [ -z "$PARAMS" ]; then
193+
printf "\t\tvar path = \"$URL\";\n"
194+
else
195+
#render throwifnull checks for the route parameters
196+
printf "$PARAMS" |
197+
sed -e 's/\w* = \([^,]*\),*/\t\t\1.ThrowIfNullOrEmpty("\1");\n/g'
198+
#string format path
199+
printf "\t\tvar path = \"$URL\".Inject(new { $PARAMS });\n"
200+
fi
201+
202+
203+
204+
# If the current verb is GET or HEAD we don't have a body to pass along
205+
if [ "$VERB" == "GET" ]; then
206+
printf "\t\treturn this.DoRequest$Async(\"$VERB\", path, queryString);\n"
207+
elif [ "$VERB" == "HEAD" ]; then
208+
printf "\t\treturn this.DoRequest$Async(\"$VERB\", path, queryString);\n"
209+
else
210+
printf "\t\treturn this.DoRequest$Async(\"$VERB\", path, body, queryString);\n"
211+
fi
212+
213+
printf "\t}\n"
106214
else
107-
echo "$LINE"
108-
fi #>> src/Generated/intermediate-definition-params.txt
215+
#Get the verb from the comment line and remember it for the next line
216+
VERB=$(echo "$LINE" | sed -e 's/^.*<summary>\(.*\) .*$/\1/')
217+
218+
# Get the route from the comment and remember it for the next line
219+
URL=$(echo "$LINE" | sed -e 's/^.*<summary>.* \(.*\)<\/summary>$/\1/')
220+
# This gets all the params in the URL and turns them into the body of an object initializer
221+
# i.e '/{x}/y/{z}' => 'x = x, y = y'
222+
PARAMS=$(echo $URL |
223+
sed -e 's/[^{]*{\([^}]*\)}/\1 = \1, /g' |
224+
sed -e 's/\/[^\/,]*//g' | sed -e 's/, $//')
225+
printf "\n\t$LINE\n"
226+
227+
fi >> src/NEST/RawElasticClient-Generated.cs
109228
done
110229

230+
cat >>src/NEST/RawElasticClient-Generated.cs <<EOL
231+
}
232+
}
233+
EOL
234+
235+
rm -f src/Generated/tmp.txt

src/Generated/intermediate-definition-params.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)