Skip to content

Commit

Permalink
provide RepairedYaml
Browse files Browse the repository at this point in the history
  • Loading branch information
mpapenbr committed Nov 19, 2023
1 parent add507c commit c93f2f5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
23 changes: 12 additions & 11 deletions irsdk/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (irsdk *Irsdk) GetYaml() (*yaml.IrsdkYaml, error) {
if err != nil {
// maybe the yaml is just not valid (see issue #6)
// let's try to fix it and try again
err := goyaml.Unmarshal([]byte(fixYaml(yamlData)), &irsdk.irsdkYaml)
err := goyaml.Unmarshal([]byte(irsdk.RepairedYaml(yamlData)), &irsdk.irsdkYaml)
if err != nil {
return nil, err
}
Expand All @@ -166,6 +166,17 @@ func (irsdk *Irsdk) GetYamlString() string {
return b.String()
}

// replaces the yaml team and user name with a quoted string
// these values are not quoted in the original yaml and most certainly cause issues
func (irsdk *Irsdk) RepairedYaml(s string) string {
work := s
for _, key := range []string{"TeamName", "UserName"} {
re := regexp.MustCompile(fmt.Sprintf("%s: (.*)", key))
work = re.ReplaceAllString(work, fmt.Sprintf("%s: \"$1\"", key))
}
return work
}

func (irsdk *Irsdk) GetValue(name string) (any, error) {
return irsdk.getValueFromBuf(name, irsdk.latestVarBuffer)
}
Expand Down Expand Up @@ -494,13 +505,3 @@ func isZeroed(buf []byte) bool {
}
return true
}

// fixYaml replaces the yaml team and user name with a quoted string
func fixYaml(s string) string {
work := s
for _, key := range []string{"TeamName", "UserName"} {
re := regexp.MustCompile(fmt.Sprintf("%s: (.*)", key))
work = re.ReplaceAllString(work, fmt.Sprintf("%s: \"$1\"", key))
}
return work
}
3 changes: 2 additions & 1 deletion irsdk/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ func Test_fixYaml(t *testing.T) {
`,
},
}
irsdk := &Irsdk{}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := fixYaml(tt.args.s); got != tt.want {
if got := irsdk.RepairedYaml(tt.args.s); got != tt.want {
t.Errorf("fixYaml() = %v, want %v", got, tt.want)
}
})
Expand Down

0 comments on commit c93f2f5

Please sign in to comment.