I never mastered regular expressions, But I had to start using it now to validate all our Json api's. I partly understand what it is doing,
But don’t want to hardcode the 20 URI formats in the sql select and still have to deal with parsing out the after the ? which has a lot of
unknows. It's easy for the web programmer they have all the library that can parsing out everything for them.
I keep thinking if I am going down the right road write everything in RPGLE.
John Slanina
On 12/6/18, 1:03 PM, "WEB400 on behalf of Tim Fathers" <web400-bounces@xxxxxxxxxxxx on behalf of X700-IX2J@xxxxxxxxxxx> wrote:
Are you just wishing to parse the URI and pull out the parameters and query strings from it? If so regular expressions (regex) are probably the most popular way of doing that and perhaps the easiest way to use regex in RPG is using embedded SQL. There are some regex APIs too, they are more difficult to use, but obviously they're likely to be faster.
Here is a really basic SQL example you can run, bear in mind though that the patterns I have used here are just simple examples, in reality they'd need to be a *lot* smarter!!:
select regexp_substr(URI, PATTERN, 1, 1, '', 1) as CUSTOMER_NUMBER, -- Match group 1
regexp_substr(URI, PATTERN, 1, 1, '', 2) as ORDER_NUMBER, -- Match group 2
regexp_substr(URI, PATTERN, 1, 1, '', 3) as PAGE_NUMBER -- Match group 3
from(values(
'/customers/1/orders/42?page=7',
'\/customers\/([0-9]{1,9})\/orders\/([0-9]{1,9})?.*=([0-9]{1,9})'
)) x(
URI,
PATTERN
)
The "URI" field is an example URI and the "PATTERN" is the regular expression pattern it is looking for. Each parameter is defined as a "matching group" by surrounding it with round brackets. I've assumed here that all the parameters are numeric and can be between 1 and 9 digits long. Group 1 is the customer number, group 2 is the order number and group 3 is the page number. The backslashes before the forward slashes are needed to escape the latter as forward slashes have a special meaning in regex patterns.
In RPG you could implement this as a loop to extract the parameters to an array as in this (completely untested!) example:
: : : : : : : :
dcl-s uriParm varchar(256);
dcl-s uriParms like(uriParm) dim(MAX_PARMS);
dcl-s group int(5);
dcl-s nullInd int(5);
for group = 1 to %elem(uriParms);
exec sql set :uriParm :nullInd = regexp_substr(URI, PATTERN, 1, 1, '', :group);
if nullInd < 0;
leave;
endif;
uriParms(group) = uriParm;
endfor;
: : : : : : : :
Tim.
________________________________
From: WEB400 <web400-bounces@xxxxxxxxxxxx> on behalf of Slanina, John <jslanina@xxxxxxxxxx>
Sent: 06 December 2018 17:59
To: web400@xxxxxxxxxxxx
Subject: [WEB400] Uri Template
We are try to switch for a free form json specification to Json api specification
https://nam03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fjsonapi.org&data=02%7C01%7C%7Cf4d9595e3cae47d7332508d65b9c4758%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636797124096034252&sdata=q1fVA%2BeP1cQGK6RWZa9y2eMu7MGOVjlXU3SI2yCB8gU%3D&reserved=0 for all are API’s
The one project I am working now is to create a URI Template System , kinda like org.springframework.web.util.UriTemplate but for the iseries.
Before all the variables where in the Json doc that was sent via the POST method and then we just used Scott’s YAJL to get the values.
Now we need to parse out the URI and look for information.
I am looking for any input on the best direction to go. We have well over a 100 Api’s we would like to change and we create more each day.
Thanks
John Slanina
--
This is the Web Enabling the IBM i (AS/400 and iSeries) (WEB400) mailing list
To post a message email: WEB400@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit:
https://nam03.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.midrange.com%2Fmailman%2Flistinfo%2Fweb400&data=02%7C01%7C%7Cf4d9595e3cae47d7332508d65b9c4758%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636797124096034252&sdata=GoBinHvilQ5epVlh06Sm82bsr01AS%2FEoyhtImYw27Zs%3D&reserved=0
or email: WEB400-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at
https://nam03.safelinks.protection.outlook.com/?url=https%3A%2F%2Farchive.midrange.com%2Fweb400&data=02%7C01%7C%7Cf4d9595e3cae47d7332508d65b9c4758%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636797124096034252&sdata=Bf9GWYRgqnJQO0W2POtep%2F1tiS%2BA4f2jvsFtx4uTzRc%3D&reserved=0.
--
This is the Web Enabling the IBM i (AS/400 and iSeries) (WEB400) mailing list
To post a message email: WEB400@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit:
https://lists.midrange.com/mailman/listinfo/web400
or email: WEB400-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at
https://archive.midrange.com/web400.
As an Amazon Associate we earn from qualifying purchases.