Skip to content

Simulate protocol with some declaration in class and json validate

License

Notifications You must be signed in to change notification settings

mesopelagique/PseudoProtocol

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PseudoProtocol

language language-top code-size build discord

The aim of this project is to simulate Protocol.

The check is at runtime and not at compilation time, too bad.

Create a protocol

First we have the Protocol class. All others protocols classes must extends it.

Class extends Protocol

Then each protocol define the function and property

Class constructor
    Super()
    This.addProperty("name"; Is text)
    This.addFunction("getRepository")

Setup the protocol store

A variable ps represent the protocol store. You must instanciate it one time and fill it with all protocols.

To do so just instantiate one times them.

ps:=New object()
cs.ServiceProtocol.new() // auto register into ps
cs.MySecondProtocol.new()

then you can access protocol with ps: ps.ServiceProtocol

or use psInit which instanciate all protocols found in class store cs.

Check if instance implement the protocol

You have an instance and want to know if all properties and functions match a protocol.

$instance:=cs.Github.new()

You could ask protocol for that:

If(ps.ServiceProtocol.isInstance($instance))
    // do something
End if

You could also assert if you code need to be called by a correct instance type

If (ps.ServiceProtocol.assertedInstance($instance))
    // do something
End if 

💡 You could do ASSERT(ps.ServiceProtocol.isInstance(This)) in your class object contructor to check immediately that it follow the protocol rules

How it work?

Just by using JSON Validate with schema validation. The protocol is the schema.


mesopelagique

About

Simulate protocol with some declaration in class and json validate

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages