Skip to content

Commit

Permalink
Forwarded JSOn should have lowercase keys
Browse files Browse the repository at this point in the history
Golang uses capitalized variable names for JSON that is sent into
the yggdrasil service. When this data is forwarded, it needs to be
converted back to the proper JSON format to retain the same structure.
  • Loading branch information
ehelms committed Jun 10, 2022
1 parent 6f1fab3 commit 794dea1
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 16 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Go

on:
push:
pull_request:

jobs:
build:
name: "Build"
runs-on: ubuntu-latest
strategy:
matrix:
go: ['1.15', '1.16', '1.17']
steps:
- uses: actions/checkout@v2

- name: Setup go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go }}

- name: Go build (source)
run: make

- name: Go build (tests)
run: make test

- name: Go lint
run: |
if [ $(gofmt -l src/* | wc -l) -gt 0 ]; then
gofmt -d src/*
exit 1
fi
- name: Go vet
run: make vet

lint-code:
name: Lint code
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v2
- name: Run golangci-lint
uses: golangci/[email protected]
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ distribution-tarball:
--transform s/^\./$(PKGNAME)-$(VERSION)/ \
. && mv /tmp/$(PKGNAME)-$(VERSION).tar.gz .
rm -rf ./vendor

test:
go test *.go

vet:
go vet *.go
38 changes: 22 additions & 16 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ type forwarderServer struct {
}

type httpMessage struct {
ResponseTo string
Metadata map[string]string
Content []byte
Directive string
ResponseTo string `json:"response_to"`
Metadata map[string]string `json:"metadata"`
Content []byte `json:"content"`
Directive string `json:"directive"`
}

// Send implements the "Send" method of the Worker gRPC service.
Expand All @@ -42,18 +42,7 @@ func (s *forwarderServer) Send(ctx context.Context, d *pb.Data) (*pb.Receipt, er
}
defer conn.Close()

// Create a data message to send back to the dispatcher.
data := httpMessage{
ResponseTo: d.GetMessageId(),
Metadata: d.GetMetadata(),
Content: d.GetContent(),
Directive: d.GetDirective(),
}

dataJson, error := json.Marshal(data)
if error != nil {
log.Fatal(error)
}
dataJson := jsonData(d)
log.Infof("sending %v", dataJson)

// Call http post
Expand All @@ -78,3 +67,20 @@ func (s *forwarderServer) Send(ctx context.Context, d *pb.Data) (*pb.Receipt, er
// Respond to the start request that the work was accepted.
return &pb.Receipt{}, nil
}

func jsonData(d *pb.Data) []byte {
// Create a data message to send back to the dispatcher.
data := httpMessage{
ResponseTo: d.GetMessageId(),
Metadata: d.GetMetadata(),
Content: d.GetContent(),
Directive: d.GetDirective(),
}

dataJson, error := json.Marshal(data)
if error != nil {
log.Fatal(error)
}

return dataJson;
}
17 changes: 17 additions & 0 deletions server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import (
pb "github.com/redhatinsights/yggdrasil/protocol"
"testing"
)

func TestDispatch(t *testing.T) {
input := &pb.Data{}

got := jsonData(input)
want := "{\"response_to\":\"\",\"metadata\":null,\"content\":null,\"directive\":\"\"}"

if string(got) != want {
t.Fatalf(`Got: %q, Wanted: %q`, string(got), want)
}
}

0 comments on commit 794dea1

Please sign in to comment.