-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This module uses `github.com/expr-lang/expr` which, because of the extensive use of the `reflect` package, disables go's compiler dead code elimination which can lead to bigger binaries. This commit adds a `noexprlang` build tag that allows jsm.go users that do not use expression matching to entirely disable the use of the expr module so that they can benefit from go's dead code elimination if they are eligible to outside jsm.go. References: - https://golab.io/talks/getting-the-most-out-of-dead-code-elimination - https://github.com/aarzilli/whydeadcode - spf13/cobra#1956 Signed-off-by: Sylvain Rabot <sylvain@abstraction.fr>
- Loading branch information
Showing
6 changed files
with
175 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
//go:build !noexprlang | ||
|
||
package jsm | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/expr-lang/expr" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
// StreamQueryExpression filters the stream using the expr expression language | ||
// Using this option with a binary built with the `noexprlang` build tag will | ||
// always return [ErrNoExprLangBuild]. | ||
func StreamQueryExpression(e string) StreamQueryOpt { | ||
return func(q *streamQuery) error { | ||
q.expression = e | ||
return nil | ||
} | ||
} | ||
|
||
func (q *streamQuery) matchExpression(streams []*Stream) ([]*Stream, error) { | ||
if q.expression == "" { | ||
return streams, nil | ||
} | ||
|
||
var matched []*Stream | ||
|
||
for _, stream := range streams { | ||
cfg := map[string]any{} | ||
state := map[string]any{} | ||
info := map[string]any{} | ||
|
||
cfgBytes, _ := yaml.Marshal(stream.Configuration()) | ||
yaml.Unmarshal(cfgBytes, &cfg) | ||
nfo, _ := stream.LatestInformation() | ||
nfoBytes, _ := yaml.Marshal(nfo) | ||
yaml.Unmarshal(nfoBytes, &info) | ||
stateBytes, _ := yaml.Marshal(nfo.State) | ||
yaml.Unmarshal(stateBytes, &state) | ||
|
||
env := map[string]any{ | ||
"config": cfg, | ||
"state": state, | ||
"info": info, | ||
"Info": nfo, | ||
} | ||
|
||
program, err := expr.Compile(q.expression, expr.Env(env), expr.AsBool()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
out, err := expr.Run(program, env) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
should, ok := out.(bool) | ||
if !ok { | ||
return nil, fmt.Errorf("expression did not return a boolean") | ||
} | ||
|
||
if should { | ||
matched = append(matched, stream) | ||
} | ||
} | ||
|
||
return matched, nil | ||
} | ||
|
||
// ConsumerQueryExpression filters the consumers using the expr expression language | ||
// Using this option with a binary built with the `noexprlang` build tag will | ||
// always return [ErrNoExprLangBuild]. | ||
func ConsumerQueryExpression(e string) ConsumerQueryOpt { | ||
return func(q *consumerQuery) error { | ||
q.expression = e | ||
return nil | ||
} | ||
} | ||
|
||
func (q *consumerQuery) matchExpression(consumers []*Consumer) ([]*Consumer, error) { | ||
if q.expression == "" { | ||
return consumers, nil | ||
} | ||
|
||
var matched []*Consumer | ||
|
||
for _, consumer := range consumers { | ||
cfg := map[string]any{} | ||
state := map[string]any{} | ||
|
||
cfgBytes, _ := yaml.Marshal(consumer.Configuration()) | ||
yaml.Unmarshal(cfgBytes, &cfg) | ||
nfo, _ := consumer.LatestState() | ||
stateBytes, _ := yaml.Marshal(nfo) | ||
yaml.Unmarshal(stateBytes, &state) | ||
|
||
env := map[string]any{ | ||
"config": cfg, | ||
"state": state, | ||
"info": state, | ||
"Info": nfo, | ||
} | ||
|
||
program, err := expr.Compile(q.expression, expr.Env(env), expr.AsBool()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
out, err := expr.Run(program, env) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
should, ok := out.(bool) | ||
if !ok { | ||
return nil, fmt.Errorf("expression did not return a boolean") | ||
} | ||
|
||
if should { | ||
matched = append(matched, consumer) | ||
} | ||
} | ||
|
||
return matched, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
//go:build noexprlang | ||
|
||
package jsm | ||
|
||
// StreamQueryExpression filters the stream using the expr expression language | ||
// Using this option with a binary built with the `noexprlang` build tag will | ||
// always return [ErrNoExprLangBuild]. | ||
func StreamQueryExpression(e string) StreamQueryOpt { | ||
return func(q *streamQuery) error { | ||
q.expression = e | ||
return ErrNoExprLangBuild | ||
} | ||
} | ||
|
||
func (q *streamQuery) matchExpression(streams []*Stream) ([]*Stream, error) { | ||
if q.expression == "" { | ||
return streams, nil | ||
} | ||
return nil, ErrNoExprLangBuild | ||
} | ||
|
||
// ConsumerQueryExpression filters the consumers using the expr expression language | ||
// Using this option with a binary built with the `noexprlang` build tag will | ||
// always return [ErrNoExprLangBuild]. | ||
func ConsumerQueryExpression(e string) ConsumerQueryOpt { | ||
return func(q *consumerQuery) error { | ||
q.expression = e | ||
return ErrNoExprLangBuild | ||
} | ||
} | ||
|
||
func (q *consumerQuery) matchExpression(consumers []*Consumer) ([]*Consumer, error) { | ||
if q.expression == "" { | ||
return consumers, nil | ||
} | ||
return nil, ErrNoExprLangBuild | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters