-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathexample_exts_test.go
50 lines (40 loc) · 1.11 KB
/
example_exts_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright 2018 Blues Inc. All rights reserved.
// Use of this source code is governed by licenses granted by the
// copyright holder including that found in the LICENSE file.
package jsonata_test
import (
"fmt"
"log"
"strings"
jsonata "github.com/blues/jsonata-go"
)
//
// This example demonstrates how to extend JSONata with
// custom functions.
//
// exts defines a function named "titlecase" which maps to
// the standard library function strings.Title. Any function,
// from the standard library or otherwise, can be used to
// extend JSONata, as long as it returns either one or two
// arguments (the second argument must be an error).
var exts = map[string]jsonata.Extension{
"titlecase": {
Func: strings.Title,
},
}
func ExampleExpr_RegisterExts() {
// Create an expression that uses the titlecase function.
e := jsonata.MustCompile(`$titlecase("beneath the underdog")`)
// Register the titlecase function.
err := e.RegisterExts(exts)
if err != nil {
log.Fatal(err)
}
// Evaluate.
res, err := e.Eval(nil)
if err != nil {
log.Fatal(err)
}
fmt.Println(res)
// Output: Beneath The Underdog
}