Skip to content

Commit 9c67531

Browse files
Cheikh SeckCheikh Seck
authored andcommitted
update readme.
1 parent 57fab36 commit 9c67531

File tree

1 file changed

+69
-2
lines changed

1 file changed

+69
-2
lines changed

cmd/momentum-cli/readme.md

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Generate RPC functions and Javascript libraries with your Go Code.
1010

1111
go get github.com/cheikhshift/momentum/cmd/momentum-cli
1212

13-
## Example
13+
## Example 1
1414
The following function will be parsed by momentum-cli :
1515

1616
// RPC
@@ -25,6 +25,52 @@ The previous func will generate JS function : (The variable definitions carry o
2525

2626
Notes : Within callback function parameter `ObjectResponse` (javascript type object) the object key `result` will have your function's result, following the named variables provided. Callback function parameter `success` (javascript type boolean) is optional and indicates a successful HTTP request to the server. If `success` is false, `ObjectResponse` will have one object key, which will be `error`. `error` is a string message of why the request has failed.
2727

28+
## Example 2
29+
An interface function will be used with this example. The following function will be parsed by momentum-cli :
30+
31+
type Stk struct {
32+
Var string
33+
}
34+
35+
// RPC
36+
func (st * Stk) TestFunction () (res string) {
37+
log.Println("hello")
38+
log.Println(st.Var)
39+
st.Var = "Newval"
40+
res = "value string"
41+
return
42+
}
43+
44+
The previous func will generate the following JS code : (The interface initializer will be available in JS)
45+
46+
/**
47+
* Stk
48+
* @namespace main
49+
* @param Obj - Object with Go interface fields.
50+
* @return Stk - Object with specified interface.
51+
*/
52+
var Stk = function(Obj) {
53+
Object.assign(this, Obj)
54+
}
55+
Stk.prototype.TestFunction = function( cb){
56+
var t = {};
57+
58+
var payload = {Obj : this, Params : t};
59+
t.Working = true;
60+
this.cb = cb;
61+
jsrequestmomentum("/momentum/funcs?name=*Stk.TestFunction", payload, "POSTJSON", ObjectCallb.bind(this))
62+
}
63+
64+
Notes : definition of `function ObjectCallb`
65+
66+
function ObjectCallb(jsonobj,success) {
67+
this.Working = false;
68+
Object.assign(this, jsonobj.Obj)
69+
this.cb(jsonobj.Result, success)
70+
}
71+
72+
73+
2874
# What is generated?
2975
Momentum will generate an additional go file. This file will have the same package name as its neighbors (go files in directory you ran command).
3076

@@ -71,6 +117,19 @@ Use this function to chain multiple generated Momentum javascript libraries.
71117
return
72118
}
73119

120+
type Stk struct {
121+
Var string
122+
}
123+
124+
// RPC
125+
func (st * Stk) TestFunction () (res string) {
126+
log.Println("hello")
127+
log.Println(st.Var)
128+
st.Var = "Newval"
129+
res = "value string"
130+
return
131+
}
132+
74133

75134
func main(){
76135
r := mux.NewRouter()
@@ -94,4 +153,12 @@ name : specifies the name of the function to invoke.
94153

95154
## Post Body
96155

97-
The body is used to pass a json of your function's variables/parameters. Each json key should match a correponding function parameter name, with that parameter's data specified as the key's value. Even if your function has no parameters, pass atleast pass an empty JSON. If no data is passed momentum will return an EOF JSON error.
156+
The body is used to pass a JSON of your function's variables/parameters. Each JSON key should match a corresponding function parameter name, with that parameter's data specified as the key's value. Even if your function has no parameters, pass at least an empty JSON (`{}`). If no data is passed momentum will return an EOF JSON error.
157+
158+
### Response format
159+
With interface methods the response format differs. Here is the schema
160+
161+
{
162+
"Obj" : {} ,// interface with updates from function
163+
"Result" : {} , // result of function. Null if function returns nothing.
164+
}

0 commit comments

Comments
 (0)