Skip to content

Commit

Permalink
Fix BOM
Browse files Browse the repository at this point in the history
  • Loading branch information
lblackstone committed Mar 23, 2021
1 parent 07ebc4f commit f42d0f0
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions sdk/go/common/workspace/loaders.go
Expand Up @@ -15,8 +15,6 @@
package workspace

import (
"fmt"
"io/ioutil"
"os"
"sync"

Expand Down Expand Up @@ -49,6 +47,27 @@ var policyPackProjectSingleton *policyPackProjectLoader = &policyPackProjectLoad
internal: map[string]*PolicyPackProject{},
}

// readFile wraps os.ReadFile and also strips the Byte-order Mark (BOM) if present.
func readFile(path string) ([]byte, error) {
b, err := os.ReadFile(path)
if err != nil {
return nil, err
}

// Strip BOM bytes if present to avoid problems with downstream parsing.
// References:
// https://github.com/spkg/bom
// https://en.wikipedia.org/wiki/Byte_order_mark
if len(b) >= 3 &&
b[0] == 0xef &&
b[1] == 0xbb &&
b[2] == 0xbf {
b = b[3:]
}

return b, nil
}

// projectLoader is used to load a single global instance of a Project config.
type projectLoader struct {
sync.RWMutex
Expand All @@ -69,12 +88,10 @@ func (singleton *projectLoader) load(path string) (*Project, error) {
return nil, err
}

b, err := ioutil.ReadFile(path)
b, err := readFile(path)
if err != nil {
return nil, err
}
fmt.Printf("***Project string(bytes):\n%s\n***End Project string(bytes)\n", string(b))
fmt.Printf("***Project bytes:\n%v\n***End Project bytes\n", b)

var project Project
err = marshaller.Unmarshal(b, &project)
Expand All @@ -87,7 +104,6 @@ func (singleton *projectLoader) load(path string) (*Project, error) {
return nil, err
}

fmt.Printf("***Project struct:\n%+v\n", project)
err = project.Validate()
if err != nil {
return nil, err
Expand Down Expand Up @@ -118,7 +134,7 @@ func (singleton *projectStackLoader) load(path string) (*ProjectStack, error) {
}

var projectStack ProjectStack
b, err := ioutil.ReadFile(path)
b, err := readFile(path)
if os.IsNotExist(err) {
projectStack = ProjectStack{
Config: make(config.Map),
Expand Down Expand Up @@ -169,7 +185,7 @@ func (singleton *pluginProjectLoader) load(path string) (*PluginProject, error)
return nil, err
}

b, err := ioutil.ReadFile(path)
b, err := readFile(path)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -209,7 +225,7 @@ func (singleton *policyPackProjectLoader) load(path string) (*PolicyPackProject,
return nil, err
}

b, err := ioutil.ReadFile(path)
b, err := readFile(path)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit f42d0f0

Please sign in to comment.