Skip to content

Commit

Permalink
Plugins: optionally allow empty input
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Dec 24, 2024
1 parent 541541b commit 6d783e1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
35 changes: 19 additions & 16 deletions provider/pipeline/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,28 @@ import (
)

type Pipeline struct {
log *util.Logger
re *regexp.Regexp
jq *gojq.Query
dflt string
unpack string
decode string
log *util.Logger
re *regexp.Regexp
jq *gojq.Query
allowEmpty bool
dflt string
unpack string
decode string
}

type Settings struct {
Regex string
Default string
Jq string
Unpack string
Decode string
AllowEmpty bool
Regex string
Default string
Jq string
Unpack string
Decode string
}

func New(log *util.Logger, cc Settings) (*Pipeline, error) {
p := &Pipeline{
log: log,
log: log,
allowEmpty: cc.AllowEmpty,
}

var err error
Expand Down Expand Up @@ -170,6 +173,10 @@ func (p *Pipeline) decodeValue(value []byte) (float64, error) {
}

func (p *Pipeline) Process(in []byte) ([]byte, error) {
if p.allowEmpty && len(bytes.TrimSpace(in)) == 0 {
return nil, nil
}

b := p.transformXML(in)

if p.re != nil {
Expand All @@ -184,10 +191,6 @@ func (p *Pipeline) Process(in []byte) ([]byte, error) {
}

if p.jq != nil {
// TODO make this optional
if len(bytes.TrimSpace(b)) == 0 {
return b, nil
}
v, err := jq.Query(p.jq, b)
if err != nil {
return b, backoff.Permanent(err)
Expand Down
2 changes: 2 additions & 0 deletions templates/definition/tariff/nordpool.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ render: |
config:
source: http
uri: https://dataportal-api.nordpoolgroup.com/api/DayAheadPrices?market=DayAhead&date={{ `{{ now.Local | date "2006-01-02" }}` }}&deliveryArea={{ .region }}&currency={{ .currency }}
allowempty: true
jq: |
.multiAreaEntries | [
.[] |
Expand All @@ -67,6 +68,7 @@ render: |
config:
source: http
uri: https://dataportal-api.nordpoolgroup.com/api/DayAheadPrices?market=DayAhead&date={{ `{{ addDate (now.Local) 0 0 1 | date "2006-01-02" }}` }}&deliveryArea={{ .region }}&currency={{ .currency }}
allowempty: true
jq: |
.multiAreaEntries | [
.[] |
Expand Down

0 comments on commit 6d783e1

Please sign in to comment.