Skip to content
This repository has been archived by the owner on Dec 27, 2023. It is now read-only.

Commit

Permalink
fix: complex structs
Browse files Browse the repository at this point in the history
  • Loading branch information
ralphg6 committed Oct 11, 2022
1 parent c6de04e commit 382b813
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 6 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ _data/
.json
.vscode

data/
data/

__debug_bin
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ require (
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/bancodobrasil/go-featws v0.0.0-20221011001318-743544d3c8a4 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ github.com/bancodobrasil/gin-monitor v1.0.0-rc1 h1:l98a1muGLLMQJ6BAudvL1263vj+26
github.com/bancodobrasil/gin-monitor v1.0.0-rc1/go.mod h1:hfOEzz5YWVYU8cdyK6O5/zGy34O1lhp2/Wfo6eMCPNg=
github.com/bancodobrasil/gin-telemetry v0.0.1-rc5 h1:0isKPZ3IHkINJLibI+bXEQ/ex6v/a0SAPmUCb/l2pLg=
github.com/bancodobrasil/gin-telemetry v0.0.1-rc5/go.mod h1:amKV8U6OFB355twaZO92vy3GYesdTpT3esi2c13HNDI=
github.com/bancodobrasil/go-featws v0.0.0-20221010233623-acdac4e32d39 h1:9YxfUdmOA5VycHSzOeaEfiKTsfnEzbLsOC7BNWrxd94=
github.com/bancodobrasil/go-featws v0.0.0-20221010233623-acdac4e32d39/go.mod h1:hOjhXXhYmMrjninF6V6w6ZAaGvLZzWs5G+wIqH+PAX4=
github.com/bancodobrasil/go-featws v0.0.0-20221011001318-743544d3c8a4 h1:GOmDN95lCh0VJoulLMVvDRrj+uYLoKGhSx02sFVOlpQ=
github.com/bancodobrasil/go-featws v0.0.0-20221011001318-743544d3c8a4/go.mod h1:hOjhXXhYmMrjninF6V6w6ZAaGvLZzWs5G+wIqH+PAX4=
github.com/bancodobrasil/healthcheck v0.0.2-rc1 h1:CcM4cZglmDokOtiaMSH9JokQqEQCsdtHQ2Ej0oqvTrg=
github.com/bancodobrasil/healthcheck v0.0.2-rc1/go.mod h1:C/YL9OERFDjg0Xa60zP2PrWBJI1N92bXlrxIqo5GagY=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
Expand Down
43 changes: 38 additions & 5 deletions services/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/bancodobrasil/featws-api/dtos"
"github.com/xanzy/go-gitlab"

"github.com/bancodobrasil/go-featws"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -352,15 +353,47 @@ func (gs *gitlabService) Fill(rulesheet *dtos.Rulesheet) (err error) {
return
}

rulesArr := strings.Split(string(bRules), "\n")
rulesFile, err := featws.Load(bRules)
if err != nil {
log.Errorf("Failed to load featws file: %v", err)
return
}

rules := make(map[string]interface{})

for _, line := range rulesArr {
if line != "" {
parts := strings.SplitN(line, "=", 2)
rules[strings.Trim(parts[0], " ")] = strings.Trim(parts[1], " ")
s := rulesFile.Section("")

for _, k := range s.Keys() {
rules[k.Name()] = k.Value()
}

for _, sname := range rulesFile.SectionStrings() {
if sname == featws.DefaultSection {
continue
}
if sname[:1] == "[" {
continue
}
sec := make(map[string]interface{})
for _, k := range rulesFile.Section(sname).Keys() {
sec[k.Name()] = k.Value()
}
rules[sname] = sec
}

for _, aname := range rulesFile.ArrayStrings() {
a := rulesFile.Array(aname)

arr := make([]map[string]interface{}, 0)

for _, s := range a.Sections() {
sec := make(map[string]interface{})
for _, k := range s.Keys() {
sec[k.Name()] = k.Value()
}
arr = append(arr, sec)
}
rules[aname[1:len(aname)-1]] = arr
}

rulesheet.Rules = &rules
Expand Down
94 changes: 94 additions & 0 deletions services/gitlab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -677,8 +677,102 @@ func TestFill(t *testing.T) {
ngl := services.NewGitlab(cfg)
ngl.Connect()
err := ngl.Fill(dto)

if (*dto.Rules)["regra"].(string) != "$test" {
t.Error("error on unmarshalling rules")
}

if err != nil {
t.Error("unexpected error")
}
}

// Functions to test fill function
func TestFillRulesSlices(t *testing.T) {
dto := SetupRulesheet()

namespace := "test"

s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/api/v4/namespaces/"+namespace {
w.Write([]byte(`{"id":1,"name":"teste", "path":"testpath"}`))
return
}

if r.Method == "GET" && r.URL.Path == "/api/v4/projects/testpath/prefix-Test" {
w.Write([]byte(`{"id":1,"description":"testeDesc","name":"teste"}`))
return
}

if r.Method == "GET" && r.URL.Path == "/api/v4/projects/1/repository/files/rules.featws" {
content := base64.StdEncoding.EncodeToString([]byte("[feat]\n condition = $test\n[[tags]]\n condition = $test"))

file := gitlab.File{
Content: content,
}
data, _ := json.Marshal(file)
w.Write(data)
return
}

if r.Method == "GET" && r.URL.Path == "/api/v4/projects/1/repository/files/parameters.json" {
content := base64.StdEncoding.EncodeToString([]byte("[]"))

file := gitlab.File{
Content: content,
}
data, _ := json.Marshal(file)
w.Write(data)
return
}

w.WriteHeader(http.StatusNotFound)
}))
defer s.Close()

cfg := SetupConfig(s)

ngl := services.NewGitlab(cfg)
ngl.Connect()
err := ngl.Fill(dto)

feat, ok := (*dto.Rules)["feat"]
if !ok {
t.Error("error on unmarshalling rules: not found feat")
return
}

featMap, ok := feat.(map[string]interface{})
if !ok {
t.Error("error on unmarshalling rules: feat is no map")
return
}

if featMap["condition"].(string) != "$test" {
t.Error("error on unmarshalling rules")
return
}

tags, ok := (*dto.Rules)["tags"]
if !ok {
t.Error("error on unmarshalling rules: not found tags array")
return
}

arr, ok := tags.([]map[string]interface{})
if !ok {
t.Error("error on unmarshalling rules: tags is no array")
return
}

if arr[0]["condition"].(string) != "$test" {
t.Error("error on unmarshalling rules")
return
}

if err != nil {
t.Error("unexpected error")
return
}
}

Expand Down

0 comments on commit 382b813

Please sign in to comment.