Skip to content

Commit

Permalink
Add lint job to CI
Browse files Browse the repository at this point in the history
  • Loading branch information
shuheiktgw committed Oct 15, 2024
1 parent 3a90d71 commit 239b83c
Show file tree
Hide file tree
Showing 22 changed files with 159 additions and 87 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ on:
- master
pull_request:
jobs:
lint:
name: lint
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v4
- name: setup Go
uses: actions/setup-go@v4
with:
go-version: "1.23"
- name: run linters
run: |
make lint
race-test:
name: Test with -race
strategy:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bin/
42 changes: 42 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
run:
timeout: 5m

linters-settings:
errcheck:
check-type-assertions: true
gci:
sections:
- "standard"
- "default"
- "prefix(github.com/goccy/go-yaml)"
- "blank"
- "dot"
gofmt:
simplify: true
govet:
disable:
- tests
misspell:
locale: US
staticcheck:
checks: ["all", "-ST1000", "-ST1005"]

linters:
disable-all: true
enable:
- errcheck
- gci
- gofmt
- gosimple
- govet
- ineffassign
- misspell
- staticcheck
- typecheck
- unused

issues:
exclude-rules:
- path: _test\.go
linters:
- staticcheck
28 changes: 28 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,31 @@ cover-html: cover
.PHONY: ycat/build
ycat/build:
go build -o ycat ./cmd/ycat

.PHONY: lint
lint: golangci-lint ## Run golangci-lint
@$(GOLANGCI_LINT) run

.PHONY: fmt
fmt: golangci-lint ## Ensure consistent code style
@go mod tidy
@go fmt ./...
@$(GOLANGCI_LINT) run --fix

## Location to install dependencies to
LOCALBIN ?= $(shell pwd)/bin
$(LOCALBIN):
mkdir -p $(LOCALBIN)

## Tool Binaries
GOLANGCI_LINT ?= $(LOCALBIN)/golangci-lint

## Tool Versions
GOLANGCI_VERSION := 1.61.0

.PHONY: golangci-lint
.PHONY: $(GOLANGCI_LINT)
golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
$(GOLANGCI_LINT): $(LOCALBIN)
@test -s $(LOCALBIN)/golangci-lint && $(LOCALBIN)/golangci-lint version --format short | grep -q $(GOLANGCI_VERSION) || \
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(LOCALBIN) v$(GOLANGCI_VERSION)
9 changes: 5 additions & 4 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
"strconv"
"strings"

"github.com/goccy/go-yaml/token"
"golang.org/x/xerrors"

"github.com/goccy/go-yaml/token"
)

var (
Expand Down Expand Up @@ -2092,18 +2093,18 @@ func Merge(dst Node, src Node) error {
err := &ErrInvalidMergeType{dst: dst, src: src}
switch dst.Type() {
case DocumentType:
node := dst.(*DocumentNode)
node, _ := dst.(*DocumentNode)
return Merge(node.Body, src)
case MappingType:
node := dst.(*MappingNode)
node, _ := dst.(*MappingNode)
target, ok := src.(*MappingNode)
if !ok {
return err
}
node.Merge(target)
return nil
case SequenceType:
node := dst.(*SequenceNode)
node, _ := dst.(*SequenceNode)
target, ok := src.(*SequenceNode)
if !ok {
return err
Expand Down
5 changes: 3 additions & 2 deletions cmd/ycat/ycat.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (
"os"

"github.com/fatih/color"
"github.com/mattn/go-colorable"

"github.com/goccy/go-yaml"
"github.com/goccy/go-yaml/lexer"
"github.com/goccy/go-yaml/printer"
"github.com/mattn/go-colorable"
)

const escape = "\x1b"
Expand Down Expand Up @@ -71,7 +72,7 @@ func _main(args []string) error {
}
}
writer := colorable.NewColorableStdout()
writer.Write([]byte(p.PrintTokens(tokens) + "\n"))
_, _ = writer.Write([]byte(p.PrintTokens(tokens) + "\n"))
return nil
}

Expand Down
27 changes: 9 additions & 18 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (d *Decoder) castToFloat(v interface{}) interface{} {

func (d *Decoder) mergeValueNode(value ast.Node) ast.Node {
if value.Type() == ast.AliasType {
aliasNode := value.(*ast.AliasNode)
aliasNode, _ := value.(*ast.AliasNode)
aliasName := aliasNode.Value.GetToken().Value
return d.anchorNodeMap[aliasName]
}
Expand Down Expand Up @@ -360,7 +360,7 @@ func (d *Decoder) resolveAlias(node ast.Node) (ast.Node, error) {
if err != nil {
return nil, err
}
n.Values[idx] = value.(*ast.MappingValueNode)
n.Values[idx], _ = value.(*ast.MappingValueNode)
}
case *ast.TagNode:
value, err := d.resolveAlias(n.Value)
Expand Down Expand Up @@ -389,7 +389,7 @@ func (d *Decoder) resolveAlias(node ast.Node) (ast.Node, error) {
if err != nil {
return nil, err
}
n.Key = key.(ast.MapKeyNode)
n.Key, _ = key.(ast.MapKeyNode)
value, err := d.resolveAlias(n.Value)
if err != nil {
return nil, err
Expand Down Expand Up @@ -476,15 +476,6 @@ func (d *Decoder) getArrayNode(node ast.Node) (ast.ArrayNode, error) {
return arrayNode, nil
}

func (d *Decoder) fileToNode(f *ast.File) ast.Node {
for _, doc := range f.Docs {
if v := d.nodeToValue(doc.Body); v != nil {
return doc.Body
}
}
return nil
}

func (d *Decoder) convertValue(v reflect.Value, typ reflect.Type, src ast.Node) (reflect.Value, error) {
if typ.Kind() != reflect.String {
if !v.Type().ConvertibleTo(typ) {
Expand Down Expand Up @@ -590,7 +581,7 @@ func (d *Decoder) deleteStructKeys(structType reflect.Type, unknownFields map[st
}

if structField.IsInline {
d.deleteStructKeys(field.Type, unknownFields)
_ = d.deleteStructKeys(field.Type, unknownFields)
} else {
delete(unknownFields, structField.RenderName)
}
Expand Down Expand Up @@ -1275,7 +1266,7 @@ func (d *Decoder) decodeStruct(ctx context.Context, dst reflect.Value, src ast.N
}
continue
}
d.setDefaultValueIfConflicted(newFieldValue, structFieldMap)
_ = d.setDefaultValueIfConflicted(newFieldValue, structFieldMap)
fieldValue.Set(d.castToAssignableValue(newFieldValue, fieldValue.Type()))
continue
}
Expand Down Expand Up @@ -1623,13 +1614,13 @@ func (d *Decoder) readersUnderDir(dir string) ([]io.Reader, error) {

func (d *Decoder) readersUnderDirRecursive(dir string) ([]io.Reader, error) {
readers := []io.Reader{}
if err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err := filepath.Walk(dir, func(path string, info os.FileInfo, _ error) error {
if !d.isYAMLFile(path) {
return nil
}
reader, err := d.fileToReader(path)
if err != nil {
return errors.Wrapf(err, "failed to get reader")
reader, readerErr := d.fileToReader(path)
if readerErr != nil {
return errors.Wrapf(readerErr, "failed to get reader")
}
readers = append(readers, reader)
return nil
Expand Down
28 changes: 14 additions & 14 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,11 +592,11 @@ func TestDecoder(t *testing.T) {
},
{
"v: [A,B,C,]",
map[string][]string{"v": []string{"A", "B", "C"}},
map[string][]string{"v": {"A", "B", "C"}},
},
{
"v: [A,1,C]",
map[string][]string{"v": []string{"A", "1", "C"}},
map[string][]string{"v": {"A", "1", "C"}},
},
{
"v: [A,1,C]",
Expand All @@ -610,11 +610,11 @@ func TestDecoder(t *testing.T) {
},
{
"v:\n - A\n - B\n - C",
map[string][]string{"v": []string{"A", "B", "C"}},
map[string][]string{"v": {"A", "B", "C"}},
},
{
"v:\n - A\n - 1\n - C",
map[string][]string{"v": []string{"A", "1", "C"}},
map[string][]string{"v": {"A", "1", "C"}},
},
{
"v:\n - A\n - 1\n - C",
Expand Down Expand Up @@ -1470,7 +1470,7 @@ items:
if err := dec.Decode(&v); err != nil {
t.Fatalf("%+v", err)
}
items := v.(map[string]interface{})["items"].([]interface{})
items, _ := v.(map[string]interface{})["items"].([]interface{})
if len(items) != 2 {
t.Fatal("failed to decode with merge key")
}
Expand Down Expand Up @@ -2152,16 +2152,16 @@ func Example_DisallowUnknownField() {

const src = `---
simple: string
complecated: string
unknown: string
`
err := yaml.NewDecoder(strings.NewReader(src), yaml.DisallowUnknownField()).Decode(&v)
fmt.Printf("%v\n", err)

// OUTPUT:
// [3:1] unknown field "complecated"
// [3:1] unknown field "unknown"
// 1 | ---
// 2 | simple: string
// > 3 | complecated: string
// > 3 | unknown: string
// ^
}

Expand Down Expand Up @@ -2712,22 +2712,22 @@ func TestDecoder_LiteralWithNewLine(t *testing.T) {
LastNode string `yaml:"last"`
}
tests := []A{
A{
{
Node: "hello\nworld",
},
A{
{
Node: "hello\nworld\n",
},
A{
{
Node: "hello\nworld\n\n",
},
A{
{
LastNode: "hello\nworld",
},
A{
{
LastNode: "hello\nworld\n",
},
A{
{
LastNode: "hello\nworld\n\n",
},
}
Expand Down
7 changes: 4 additions & 3 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ import (
"strings"
"time"

"golang.org/x/xerrors"

"github.com/goccy/go-yaml/ast"
"github.com/goccy/go-yaml/internal/errors"
"github.com/goccy/go-yaml/parser"
"github.com/goccy/go-yaml/printer"
"github.com/goccy/go-yaml/token"
"golang.org/x/xerrors"
)

const (
Expand Down Expand Up @@ -93,10 +94,10 @@ func (e *Encoder) EncodeContext(ctx context.Context, v interface{}) error {
e.written = true
} else {
// write document separator
e.writer.Write([]byte("---\n"))
_, _ = e.writer.Write([]byte("---\n"))
}
var p printer.Printer
e.writer.Write(p.PrintNode(node))
_, _ = e.writer.Write(p.PrintNode(node))
return nil
}

Expand Down
11 changes: 5 additions & 6 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import (
"testing"
"time"

"github.com/goccy/go-yaml/parser"

"github.com/goccy/go-yaml"
"github.com/goccy/go-yaml/ast"
"github.com/goccy/go-yaml/parser"
)

var zero = 0
Expand Down Expand Up @@ -683,12 +682,12 @@ func TestEncoder(t *testing.T) {
// time value
{
"v: 0001-01-01T00:00:00Z\n",
map[string]time.Time{"v": time.Time{}},
map[string]time.Time{"v": {}},
nil,
},
{
"v: 0001-01-01T00:00:00Z\n",
map[string]*time.Time{"v": &time.Time{}},
map[string]*time.Time{"v": {}},
nil,
},
{
Expand Down Expand Up @@ -1078,7 +1077,7 @@ func TestEncoder_FlowRecursive(t *testing.T) {
M map[string][]int `yaml:",flow"`
}
v.M = map[string][]int{
"test": []int{1, 2, 3},
"test": {1, 2, 3},
}
var buf bytes.Buffer
if err := yaml.NewEncoder(&buf).Encode(v); err != nil {
Expand Down Expand Up @@ -1188,7 +1187,7 @@ func TestEncoder_MarshalAnchor(t *testing.T) {
hostIdx := 1
opt := yaml.MarshalAnchor(func(anchor *ast.AnchorNode, value interface{}) error {
if _, ok := value.(*Host); ok {
nameNode := anchor.Name.(*ast.StringNode)
nameNode, _ := anchor.Name.(*ast.StringNode)
nameNode.Value = fmt.Sprintf("host%d", hostIdx)
hostIdx++
}
Expand Down
Loading

0 comments on commit 239b83c

Please sign in to comment.