Quantcast
Channel: MikroTik
Viewing all articles
Browse latest Browse all 15394

Scripting • Re: Built in function library

$
0
0
While we wait for the builtin functions:

https://github.com/merlinthemagic/MTM-R ... -Scripting

Can manipulate strings, files, md5 hashing, create GUIDs etc.
@merlinthemagic7 thx for the nice compilation of missing basic functions :D

I'm trying to understand the principle structure, reading:
  1. https://github.com/merlinthemagic/MTM-R ... e-examples
  2. https://github.com/merlinthemagic/MTM-R ... trings.rsc
Instead of writing single functions you put all in a big array and call the function an array item?
That is how In understand these extracts:
Code:
##trim string example:local strTool [($MtmFacts->"get") "getTools()->getTypes()->getStrings()"];:local myStr " My string with leading and traling spaces and line breaks and chr returns \n\r";:put [($strTool->"trim") $myStr]; #string "My string with leading and traling spaces and line breaks and chr returns"
and
Code:
:set ($s->"trim") do={:global MtmFacts;:local cPath "MTM/Tools/DataTypes/Strings.rsc/trim";:if ([:typeof $0...
Hi PackElend,

It s not one large array, but a bunch of global variables each containing an array. Corruption happens when an array gets too large. But in essense you are correct the functions are stored as items in an array.

The basic design is to attempts to mimic objects and having the ability to call methods on those objects.

Everything starts with the $MtmFacts global. https://github.com/merlinthemagic/MTM-R ... /Facts.rsc
That is the only file you ever have to use the /import command to load as it will locate and load all other "classes".
Code:
/import flash/MTM/Facts.rsc;:global MtmFacts;
Lets walk through your example.
Code:
##trim string example:local strTool [($MtmFacts->"get") "getTools()->getTypes()->getStrings()"];

Here we are calling the "get" method on the $MtmFacts "object". That method takes a "class path" where the path is separated by "->", the factory will recurse each class until it gets to the final class you want. In this case the string tool:

getTools(): loads the tools factory. https://github.com/merlinthemagic/MTM-R ... /Tools.rsc

Then calls getTypes() method on the tools factory, which returns another factory: https://github.com/merlinthemagic/MTM-R ... /Types.rsc

Then calls getStrings() method on the types factory, which returns the tool singleton class: https://github.com/merlinthemagic/MTM-R ... trings.rsc

You could do the same as above, but using with individual calls like so:
Code:
:local toolFact [($MtmFacts->"getTools")];:local typeFact [($toolFact->"getTypes")];:local strTool [($typeFact->"getStrings")];

In both cases what you end up with is a variable ("object") called $strTool that you can now use to call methods on as many times as you like:
Code:
:put [($strTool->"trim") "    my string with spaces at each end  "]; ##trims the leading and trailing spaces:put [($strTool->"getRandom") 64]; ##gives you a random string of length 64:put [($strTool->"toLower") "MY strIng WiTH mixED cases"]; ##my string with mixed cases
You get the point. Helpful to have this implementation when doing loops and you need to e.g. trim each item in the loop.

All in all it is a way to organize tools we use all the time in a tree structure and separate each class into its own file. If you are familiar with OOP you will understand the factory pattern, readability, code reuseetc.

Its quite easy to add classes and methods, for example if you have built a class that can do SHA256:

Add another method to the Hashing factory called "getSHA256" (just copy "getMD5"):
https://github.com/merlinthemagic/MTM-R ... ashing.rsc

Add a file called SHA256.rsc in:
https://github.com/merlinthemagic/MTM-R ... ls/Hashing

Have a single method on your class called "get" (see MD5.rsc for example).

Submit a pull request and now everyone can hash strings with your method like this:
Code:
:local toolObj [($MtmFacts->"get") "getTools()->getHashing()->getSHA256()"];:local myStr "My string";:put ([($toolObj->"get") $myStr]); #3f9a07d83c604dba400d13df4d34566b78338804f0b3181d4e02089fe4daa7b0

Statistics: Posted by merlinthemagic7 — Sat Mar 30, 2024 12:37 pm



Viewing all articles
Browse latest Browse all 15394

Trending Articles