Skip to content

Commit

Permalink
fix: update the reply for the manager
Browse files Browse the repository at this point in the history
Signed-off-by: saltbo <[email protected]>
  • Loading branch information
saltbo committed Dec 17, 2024
1 parent ec9ee56 commit 1d2c168
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 18 deletions.
27 changes: 27 additions & 0 deletions apis/message/v1alpha1/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package v1alpha1

import (
"encoding/json"
"strings"

"gopkg.in/yaml.v3"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand All @@ -20,3 +24,26 @@ type Reply struct {
Text string `json:"text"`
JSON any `json:"json"`
}

func (r *Reply) YAMLString() string {
out, err := yaml.Marshal(r)
if err != nil {
return ""
}

// yaml.Marshal Wrap the string in single quotes,
// but we don't need it because after wrapping, characters such as newlines cannot be parsed
return strings.ReplaceAll(string(out), "'", "")
}

func (r *Reply) String() string {
if r.Text != "" {
return r.Text
}

v, err := json.Marshal(r.JSON)
if err != nil {
return ""
}
return string(v)
}
24 changes: 6 additions & 18 deletions pkg/messenger/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package messenger
import (
"bytes"
"context"
"encoding/json"
"fmt"
"strconv"
"text/template"

"github.com/Masterminds/sprig/v3"
"github.com/samber/lo"
"gopkg.in/yaml.v3"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"

Expand Down Expand Up @@ -63,32 +62,21 @@ func (d *DefaultManager) Match(params map[string]any) (*v1alpha1.Message, error)
}

func (d *DefaultManager) BuildReply(msg *v1alpha1.Message, params map[string]any) ([]byte, error) {
msgTemplate := msg.Spec.Reply.Text
if msg.Spec.Reply.JSON != nil {
data, err := json.Marshal(msg.Spec.Reply.JSON)
if err != nil {
return nil, fmt.Errorf("encode reply.json failed: %w", err)
}

msgTemplate = string(data)
}

funcMap := sprig.TxtFuncMap()
for k, f := range d.funcMap {
funcMap[k] = f
}

buf := bytes.NewBufferString("")
t := template.Must(template.New("msg").Funcs(funcMap).Parse(msgTemplate))
t := template.Must(template.New("msg").Funcs(funcMap).Parse(msg.Spec.Reply.YAMLString()))
if err := t.Execute(buf, params); err != nil {
return nil, fmt.Errorf("render message: %w", err)
}

newMsg := buf.String()
if strconv.CanBackquote(newMsg) {
return buf.Bytes(), nil
var reply v1alpha1.Reply
if err := yaml.Unmarshal(buf.Bytes(), &reply); err != nil {
return nil, err
}

result := strconv.Quote(newMsg)
return []byte(result)[1 : len(result)-1], nil
return []byte(reply.String()), nil
}
23 changes: 23 additions & 0 deletions pkg/messenger/manager_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package messenger

import (
"fmt"
"strconv"
"testing"
"text/template"

"github.com/stretchr/testify/assert"
)

const stringContainNewline = `aaa
bbb
ccc
`

func TestDefaultManager_BuildReply(t *testing.T) {
fmt.Println(strconv.Quote(stringContainNewline)[1 : len(stringContainNewline)-1])
tmpl, err := template.New("").Parse(stringContainNewline)
assert.NoError(t, err)

tmpl.Execute(nil, nil)
}

0 comments on commit 1d2c168

Please sign in to comment.