OILS / spec / ysh-funcs-external.test.sh View on Github | oilshell.org

86 lines, 32 significant
1## our_shell: ysh
2## oils_failures_allowed: 3
3
4#### JSON func on top of proc
5
6proc myadd {
7 json read (&args)
8
9 # convenient!
10 redir >&2 {
11 = args
12 }
13
14 json write (args[0] + args[1])
15}
16
17echo '[1, 2]' | myadd | json read (&result)
18
19# TODO:
20# Rewrite this as:
21#
22# var result = myadd(1,2)
23#
24# Is that possible? I think functions can live in their own namespace?
25# Or you can have expr.Func() with all the metadata?
26# Problem with 'source mytool.hay' is that it can define funcs ANYWHERE
27# you might need a namespace like extern::mytool::myfunc()
28
29echo "result = $result"
30
31## STDOUT:
32TODO
33result = 3
34## END
35
36#### QTT func on top of proc
37
38proc myadd {
39
40 # I thought this was 'wok'
41 qtt read-rows {
42 if (_index === 0) {
43 # could write header first here
44 write --sep $TAB -- $_schema_str 'result:Int'
45
46 # _schema is the raw schema
47 }
48 # or use BEGIN and 'when', like awk-style
49
50 const result = _row->x + _row->y
51 write --sep $TAB -- $_row_str $result
52 }
53}
54
55qtt tabify '''
56x:Int y:Int
571 2
583 4
59''' | myadd | qtt read-cols result
60
61echo "result = $result"
62
63## STDOUT:
64## END
65
66#### QTT func on top of wok
67
68wok foo {
69 BEGIN {
70 write --sep $TAB -- $_schema_str' result:Int'
71 }
72
73 # for all rows. Could be 'each' or 'row'
74 when {
75 const result = _row->x + _row->y
76 write --sep $TAB -- $_row_str $result
77 }
78}
79
80## STDOUT:
81## END
82
83
84# Notes:
85# - consider JSON-RPC
86# - consider multiple return values