Skip to content

Commit

Permalink
Workaround for invalid yaml
Browse files Browse the repository at this point in the history
Fixes #6
  • Loading branch information
mpapenbr committed Nov 17, 2023
1 parent fa34195 commit add507c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 6 deletions.
27 changes: 21 additions & 6 deletions irsdk/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/http"
"os"
"reflect"
"regexp"
"strings"
"syscall"
"time"
Expand Down Expand Up @@ -112,11 +113,9 @@ func (irsdk *Irsdk) WaitForValidData() bool {
//
//nolint:lll //by design
func (irsdk *Irsdk) GetData() bool {
start := time.Now()
ret := irsdk.WaitForValidData()
delta := time.Since(start)
if ret || delta > 0 {
// dummy
if !ret {
return false
}

latestBuf := irsdk.hdr.VarBufs[0]
Expand All @@ -140,9 +139,15 @@ func (irsdk *Irsdk) GetData() bool {
}

func (irsdk *Irsdk) GetYaml() (*yaml.IrsdkYaml, error) {
err := goyaml.Unmarshal([]byte(irsdk.GetYamlString()), &irsdk.irsdkYaml)
yamlData := irsdk.GetYamlString()
err := goyaml.Unmarshal([]byte(yamlData), &irsdk.irsdkYaml)
if err != nil {
return nil, err
// 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)
if err != nil {
return nil, err
}
}
return &irsdk.irsdkYaml, nil
}
Expand Down Expand Up @@ -489,3 +494,13 @@ 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
}
50 changes: 50 additions & 0 deletions irsdk/api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package irsdk

import (
"testing"
)

func Test_fixYaml(t *testing.T) {
type args struct {
s string
}
tests := []struct {
name string
args args
want string
}{
// TODO: Add test cases.
{
"standard",
args{`
DriverInfo:
DriverCarIdx: 0
Drivers:
- CarIdx: 0
UserName: John Doe
TeamName: @Invalid Yaml team name
- CarIdx: 1
UserName: Jane Doe
TeamName: Valid Yaml team name
`},
`
DriverInfo:
DriverCarIdx: 0
Drivers:
- CarIdx: 0
UserName: "John Doe"
TeamName: "@Invalid Yaml team name"
- CarIdx: 1
UserName: "Jane Doe"
TeamName: "Valid Yaml team name"
`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := fixYaml(tt.args.s); got != tt.want {
t.Errorf("fixYaml() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit add507c

Please sign in to comment.