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 9b27bc7
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions sdk/go/common/workspace/loaders.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package workspace

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

Expand Down Expand Up @@ -49,6 +48,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 +89,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 Down Expand Up @@ -118,7 +136,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 +187,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 +227,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 9b27bc7

Please sign in to comment.