Skip to content

Commit

Permalink
Created a simple sub language example
Browse files Browse the repository at this point in the history
  • Loading branch information
generikvault committed Dec 13, 2020
1 parent bb6a60f commit e3d61a8
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ func (s *exampleCustomSelector) SelectGVal(ctx context.Context, k string) (inter
return nil, nil
}

func ExampleCustomSelector() {
func ExampleSelector() {
lang := gval.Base()
value, err := lang.Evaluate(
"myStruct.hidden",
Expand All @@ -422,3 +422,29 @@ func ExampleCustomSelector() {
// Output:
// hello world
}

func parseSub(ctx context.Context, p *gval.Parser) (gval.Evaluable, error) {
return p.ParseSublanguage(ctx, subLang)
}

var (
superLang = gval.NewLanguage(
gval.PrefixExtension('$', parseSub),
)
subLang = gval.NewLanguage(
gval.Init(func(ctx context.Context, p *gval.Parser) (gval.Evaluable, error) { return p.Const("hello world"), nil }),
)
)

func ExampleParser_ParseSublanguage() {
value, err := superLang.Evaluate("$", nil)

if err != nil {
fmt.Println(err)
}

fmt.Println(value)

// Output:
// hello world
}

0 comments on commit e3d61a8

Please sign in to comment.