[haXe] XAPI (Ximple API)
John A. De Goes
john at n-brain.net
Sun Jan 31 18:48:45 CET 2010
This API is better organized and easier to understand than the haxe std lib, but you can do even better.
Designers generally make two errors when creating file system APIs:
1. A string or other single abstraction is used to represent everything in the file system, thus losing the benefits of strong typing.
2. The file system operations are hard coded to native file system operations, meaning if you later want your code to work with FTP, HTTP or another file system, you have to completely change all of your code. Also, it's not easy to write unit tests for some code, because it requires interacting with the real file system.
Fortunately, both errors can be solved in straightforward fashion:
1. "FSNode" should be the base class representing a node in the file system, from which File, Folder, FileLink, FolderLink derive (FileLink derives from File, FolderLink derives from Folder). The internal representation should be URI-based (e.g.: file://foo.txt). The scheme of the URI ("file" in this case) should be used internally by any methods that interact with the file system, such as File.instream (which might return an InputStream). In particular, a method node.x(...) is just shorthand for: SchemeHandler.forScheme(scheme).x(node, ...). For example, File.instream is shorthand for SchemeHandler.forScheme(myFile.scheme).instream(myFile).
2. SchemeHandler should be defined as an abstract class with all file system-related methods (reading files, writing files, moving files, retrieving permissions, setting permissions, etc.). It should also have several static methods: SchemeHandler.forScheme, which retrieves the particular scheme handler for the specified scheme; SchemeHandler.install, which installs a new scheme handler; SchemeHandler.listAll, which lists installed scheme handlers; and SchemeHandler.resolve, which resolves a string to an FSNode, based on the currently installed scheme handlers (the act of resolving requires determining whether something is a file, folder, file link, or folder link, so it depends on whether or not a scheme handler has been installed that can provide such information).
3. SchemeHandler should come pre-initialized with a SchemeHandlerFile, which extends SchemeHandler for the "file" scheme. Over time other implementations can be written: test (for writing unit tests), FTP, HTTP, SFTP, S3, etc.
This will provide a strongly-typed file system API, which will eliminate many errors at compile time, while also being extensible to handle any number of schemes. And if you code the API correctly, it can also be easy to use (though never quite as easy to use as a dumb API, you do pay a small price for the increase in flexibility).
Regards,
John
On Jan 31, 2010, at 2:16 AM, Juan Delgado wrote:
> Hi there,
>
> I like ?filter String -> Bool and leaving up to people implementing it
> with a RegExp or whatever. Nice one.
>
> 2 things though:
>
> 1) Make it default to anything? As I say, maybe exclude hidden files
> and folders by default. But I'm happy either way.
>
> 2) I still prefer using ?deep : Int = 0 for defining the levels of
> recursion. I know this is a little bit less intuitive but much more
> flexible in the long run. I got this idea from bash's command find
> which you can tune with -maxdepth and -mindepth.
>
> Find attached a new draft of the API with these changes and some other
> minor adjustments I did yesterday night (mostly name simplifications
> and re-arranging of some functions).
>
> Thanks a lot!
>
> Juan
>
> 2010/1/31 Heinz Hölzer <heinz.hoelzer at googlemail.com>:
>> Am 31.01.2010 05:19, schrieb Yanis Benson:
>>
>> On 01/31/2010 07:06 AM, Heinz Hölzer wrote:
>>
>> i would prefer:
>>
>> copy (source:String, destination:String, ?exclude : String -> Bool, ?include
>> : String -> Bool, ?recursive : Bool = false):Void
>>
>> cheers
>> heinz
>>
>> WHat is exclude needed for then? One function can make it all.
>>
>>
>>
>> that's true ;)
>>
>> so it's
>>
>> copy (source:String, destination:String, ?filter : String -> Bool,
>> ?recursive : Bool = false):Void
>>
>> --
>> haXe - an open source web programming language
>> http://haxe.org
>>
>
>
>
> --
> Juan Delgado - Zárate
> http://zarate.tv
> http://blog.zarate.tv
> <xapi.txt>--
> haXe - an open source web programming language
> http://haxe.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.motion-twin.com/pipermail/haxe/attachments/20100131/134f54b2/attachment-0001.htm
More information about the Haxe
mailing list