Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error handling w/o error slices and more #157

Closed
wants to merge 44 commits into from
Closed
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
c6f0308
initial error handling refactoring
jxsl13 Aug 19, 2023
1cc2a40
fix some tests
jxsl13 Aug 19, 2023
bd65eb9
use filter func that is initialized only once
jxsl13 Aug 19, 2023
82f80e3
return broken object upon error as expected in the tests
jxsl13 Aug 20, 2023
a3eb526
fix example
jxsl13 Aug 20, 2023
8bc8538
update lower level tests
jxsl13 Aug 20, 2023
23ca2ac
do not return objects with errors
jxsl13 Aug 20, 2023
f64b868
fix tests, revert to returning objects even tho we have errors (circu…
jxsl13 Aug 20, 2023
18902b9
update ci go version to match go.mod version
jxsl13 Aug 20, 2023
b11b31a
harden ci pipeline
jxsl13 Aug 20, 2023
beed338
add dependabot
jxsl13 Aug 20, 2023
6b2595b
go fmt ./...
jxsl13 Aug 20, 2023
1a740b7
fix some linter warnings
jxsl13 Aug 20, 2023
1d14787
remove redundant break statements
jxsl13 Aug 20, 2023
f0d8af0
compile regex only once
jxsl13 Aug 20, 2023
2af5be8
use copy intead of for loop
jxsl13 Aug 20, 2023
3e55a41
fix some linter warnings
jxsl13 Aug 20, 2023
987651d
fix some linter warnings
jxsl13 Aug 20, 2023
06876cb
compile regex only once
jxsl13 Aug 20, 2023
5438afe
trim suffix with stdlib
jxsl13 Aug 20, 2023
127b681
update test
jxsl13 Aug 20, 2023
ecf7db3
allow reference resolving by default
jxsl13 Aug 20, 2023
745af79
update test to accomodate the default value change
jxsl13 Aug 20, 2023
38d5c31
fix example
jxsl13 Aug 20, 2023
d8abbdb
update MultiError
jxsl13 Aug 20, 2023
d9b4b37
try to keep MultiError flat
jxsl13 Aug 20, 2023
c48b1bd
remove unused error mapper functions
jxsl13 Aug 20, 2023
c4fcda1
decrease error handling complexity
jxsl13 Aug 20, 2023
4381bee
add errorutils tests
jxsl13 Aug 20, 2023
9da55d5
make zero value config struct the same as the default config
jxsl13 Aug 20, 2023
c3aa80c
test splitting test, race & coverage
jxsl13 Aug 26, 2023
e73fc93
Merge branch 'main' into poc/error-handling
jxsl13 Aug 26, 2023
b434c89
update go.sum
jxsl13 Aug 26, 2023
8c2c821
try w/o timeout
jxsl13 Aug 26, 2023
f6735fe
add makefile with the complete test commands
jxsl13 Aug 26, 2023
63207d2
add t.Parallel to tests that don't have global shared stats
jxsl13 Aug 26, 2023
9ec2200
rm -race flag
jxsl13 Aug 30, 2023
af5bb0d
Merge branch 'main' into poc/error-handling
jxsl13 Sep 1, 2023
6381834
fix build.yaml
jxsl13 Sep 1, 2023
04407cc
fix multiple calls to t.Parallel()
jxsl13 Sep 1, 2023
e834c01
and filter logic -> or filter logic
jxsl13 Sep 1, 2023
d2a8bc8
improve configuration & add tests
jxsl13 Sep 1, 2023
b842c44
improve filter & make tests more understandable
jxsl13 Sep 1, 2023
c9cb487
call all option functions for more test coverage
jxsl13 Sep 1, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .github/dependabot.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: 2
updates:
- package-ecosystem: gomod
directory: /
schedule:
interval: weekly
reviewers:
- "daveshanley"

- package-ecosystem: github-actions
directory: /
schedule:
interval: monthly
reviewers:
- "daveshanley"
101 changes: 67 additions & 34 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -1,49 +1,82 @@
name: Build
name: Build & Test

on:
push:
branches:
- main
branches: [ "main" ]
paths:
- '**.go'
- '**.yaml'
- '**.yml'
- '**.toml'
- '**.json'
- 'go.mod'
- 'go.sum'
pull_request:
branches:
- main
# The branches below must be a subset of the branches above
branches: [ "main" ]
paths:
- '**.go'
- '**.yaml'
- '**.yml'
- '**.toml'
- '**.json'
- 'go.mod'
- 'go.sum'

jobs:
build:
name: Build
runs-on: ubuntu-latest
name: Build & Test
strategy:
matrix:
go-version: ['stable', 'oldstable']
platform: ['ubuntu-latest']
runs-on: ${{ matrix.platform }}
permissions:
# required for security related workflows
security-events: write
# only required for workflows in private repositories
actions: read
contents: read
steps:
- name: Set up Go 1.x
uses: actions/setup-go@v3
with:
go-version: 1.19
id: go

- name: Checkout code
uses: actions/checkout@v3

- name: Get dependencies
run: |
go get -v -t -d ./...
if [ -f Gopkg.toml ]; then
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
dep ensure
fi
- name: Test
run: go test ./...
- name: Coverage
run: |
go get github.com/axw/gocov/gocov
go get github.com/AlekSi/gocov-xml
go install github.com/axw/gocov/gocov
go install github.com/AlekSi/gocov-xml
- run: |
go test -v -coverprofile cover.out ./...
gocov convert cover.out | gocov-xml > coverage.xml
- uses: codecov/codecov-action@v3
- name: Install Go
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}

- name: Vet
run: go vet ./...

- name: Install govulncheck
run: go install golang.org/x/vuln/cmd/govulncheck@latest

- name: Run govulncheck
run: govulncheck ./...

- name: Run Gosec Security Scanner
uses: securego/gosec@master
with:
# we let the report trigger content trigger a failure using the GitHub Security features.
args: '-no-fail -fmt sarif -out results.sarif ./...'
- name: Upload SARIF file
uses: github/codeql-action/upload-sarif@v2
with:
# Path to SARIF file relative to the root of the repository
sarif_file: results.sarif

- name: Build
run: go build ./...

- name: Test & Code Coverage
run: go test ./... -v -timeout 20m -race -count=1 -covermode=atomic -coverprofile=coverage.txt

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: ./coverage.xml
flags: unittests
files: ./coverage.txt
fail_ci_if_error: false
verbose: true

3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
test-operation.yaml
.idea/
*.iml
*.iml
__debug_bin*
2 changes: 1 addition & 1 deletion datamodel/high/base/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ func NewSchema(schema *base.Schema) *Schema {
completeChildren := 0
if children > 0 {
allDone:
for true {
for {
select {
case <-polyCompletedChan:
completeChildren++
Expand Down
34 changes: 17 additions & 17 deletions datamodel/high/base/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,10 +499,10 @@ required: [cake, fish]`
assert.NotNil(t, compiled)
assert.Nil(t, schemaProxy.GetBuildError())

assert.True(t, compiled.ExclusiveMaximum.A)
assert.Equal(t, float64(123), compiled.Properties["somethingB"].Schema().ExclusiveMinimum.B)
assert.Equal(t, float64(334), compiled.Properties["somethingB"].Schema().ExclusiveMaximum.B)
assert.Len(t, compiled.Properties["somethingB"].Schema().Properties["somethingBProp"].Schema().Type, 2)
assert.True(t, compiled.ExclusiveMaximum.A)
assert.Equal(t, float64(123), compiled.Properties["somethingB"].Schema().ExclusiveMinimum.B)
assert.Equal(t, float64(334), compiled.Properties["somethingB"].Schema().ExclusiveMaximum.B)
assert.Len(t, compiled.Properties["somethingB"].Schema().Properties["somethingBProp"].Schema().Type, 2)

assert.Equal(t, "nice", compiled.AdditionalProperties.(*SchemaProxy).Schema().Description)

Expand Down Expand Up @@ -587,25 +587,25 @@ type: number
}

func TestSchemaNumberMultipleOfInt(t *testing.T) {
yml := `
yml := `
type: number
multipleOf: 5
`
highSchema := getHighSchema(t, yml)

value := float64(5)
assert.EqualValues(t, &value, highSchema.MultipleOf)
assert.EqualValues(t, &value, highSchema.MultipleOf)
}

func TestSchemaNumberMultipleOfFloat(t *testing.T) {
yml := `
yml := `
type: number
multipleOf: 0.5
`
highSchema := getHighSchema(t, yml)
highSchema := getHighSchema(t, yml)

value := 0.5
assert.EqualValues(t, &value, highSchema.MultipleOf)
value := 0.5
assert.EqualValues(t, &value, highSchema.MultipleOf)
}

func TestSchemaNumberMinimumInt(t *testing.T) {
Expand All @@ -616,17 +616,17 @@ minimum: 5
highSchema := getHighSchema(t, yml)

value := float64(5)
assert.EqualValues(t, &value, highSchema.Minimum)
assert.EqualValues(t, &value, highSchema.Minimum)
}

func TestSchemaNumberMinimumFloat(t *testing.T) {
yml := `
yml := `
type: number
minimum: 0.5
`
highSchema := getHighSchema(t, yml)
highSchema := getHighSchema(t, yml)

value := 0.5
value := 0.5
assert.EqualValues(t, &value, highSchema.Minimum)
}

Expand All @@ -638,7 +638,7 @@ minimum: 0
highSchema := getHighSchema(t, yml)

value := float64(0)
assert.EqualValues(t, &value, highSchema.Minimum)
assert.EqualValues(t, &value, highSchema.Minimum)
}

func TestSchemaNumberExclusiveMinimum(t *testing.T) {
Expand All @@ -661,7 +661,7 @@ maximum: 5
highSchema := getHighSchema(t, yml)

value := float64(5)
assert.EqualValues(t, &value, highSchema.Maximum)
assert.EqualValues(t, &value, highSchema.Maximum)
}

func TestSchemaNumberMaximumZero(t *testing.T) {
Expand All @@ -672,7 +672,7 @@ maximum: 0
highSchema := getHighSchema(t, yml)

value := float64(0)
assert.EqualValues(t, &value, highSchema.Maximum)
assert.EqualValues(t, &value, highSchema.Maximum)
}

func TestSchemaNumberExclusiveMaximum(t *testing.T) {
Expand Down
26 changes: 7 additions & 19 deletions datamodel/high/node_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ package high

import (
"fmt"
"github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/utils"
"gopkg.in/yaml.v3"
"reflect"
"sort"
"strconv"
"strings"
"unicode"

"github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/utils"
"gopkg.in/yaml.v3"
)

// NodeEntry represents a single node used by NodeBuilder.
Expand Down Expand Up @@ -197,7 +198,6 @@ func (n *NodeBuilder) add(key string, i int) {
})
nodeEntry.Line = lines[0].line // pick the lowest line number so this key is sorted in order.
nodeEntry.Style = lines[0].style
break
case reflect.Map:
l := value.Len()
line := make([]int, l)
Expand Down Expand Up @@ -303,8 +303,6 @@ func (n *NodeBuilder) AddYAMLNode(parent *yaml.Node, entry *NodeEntry) *yaml.Nod
valueNode = utils.CreateStringNode(val)
valueNode.Line = line
valueNode.Style = entry.Style
break

case reflect.Bool:
val := value.(bool)
if !val {
Expand All @@ -313,26 +311,18 @@ func (n *NodeBuilder) AddYAMLNode(parent *yaml.Node, entry *NodeEntry) *yaml.Nod
valueNode = utils.CreateBoolNode("true")
}
valueNode.Line = line
break

case reflect.Int:
val := strconv.Itoa(value.(int))
valueNode = utils.CreateIntNode(val)
valueNode.Line = line
break

case reflect.Int64:
val := strconv.FormatInt(value.(int64), 10)
valueNode = utils.CreateIntNode(val)
valueNode.Line = line
break

case reflect.Float32:
val := strconv.FormatFloat(float64(value.(float32)), 'f', 2, 64)
valueNode = utils.CreateFloatNode(val)
valueNode.Line = line
break

case reflect.Float64:
precision := -1
if entry.StringValue != "" && strings.Contains(entry.StringValue, ".") {
Expand All @@ -341,8 +331,6 @@ func (n *NodeBuilder) AddYAMLNode(parent *yaml.Node, entry *NodeEntry) *yaml.Nod
val := strconv.FormatFloat(value.(float64), 'f', precision, 64)
valueNode = utils.CreateFloatNode(val)
valueNode.Line = line
break

case reflect.Map:

// the keys will be rendered randomly, if we don't find out the original line
Expand Down Expand Up @@ -373,7 +361,7 @@ func (n *NodeBuilder) AddYAMLNode(parent *yaml.Node, entry *NodeEntry) *yaml.Nod
fg := reflect.ValueOf(pr.GetValueUntyped())
found := false
found, orderedCollection = n.extractLowMapKeys(fg, x, found, orderedCollection, m, k)
if found != true {
if !found {
// this is something new, add it.
orderedCollection = append(orderedCollection, &NodeEntry{
Tag: x,
Expand Down Expand Up @@ -456,13 +444,13 @@ func (n *NodeBuilder) AddYAMLNode(parent *yaml.Node, entry *NodeEntry) *yaml.Nod
if er, ko := sqi.(Renderable); ko {
var rend interface{}
if !n.Resolve {
rend, _ = er.(Renderable).MarshalYAML()
rend, _ = er.MarshalYAML()
} else {
// try and render inline, if we can, otherwise treat as normal.
if _, ko := er.(RenderableInline); ko {
rend, _ = er.(RenderableInline).MarshalYAMLInline()
} else {
rend, _ = er.(Renderable).MarshalYAML()
rend, _ = er.MarshalYAML()
}
}
// check if this is a pointer or not.
Expand Down
7 changes: 4 additions & 3 deletions datamodel/high/v2/swagger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@
package v2

import (
"os"

"github.com/pb33f/libopenapi/datamodel"
v2 "github.com/pb33f/libopenapi/datamodel/low/v2"
"github.com/stretchr/testify/assert"

"io/ioutil"
"testing"
)

var doc *v2.Swagger

func initTest() {
data, _ := ioutil.ReadFile("../../../test_specs/petstorev2-complete.yaml")
data, _ := os.ReadFile("../../../test_specs/petstorev2-complete.yaml")
info, _ := datamodel.ExtractSpecInfo(data)
var err []error
var err error
doc, err = v2.CreateDocument(info)
if err != nil {
panic("broken something")
Expand Down
3 changes: 1 addition & 2 deletions datamodel/high/v3/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,7 @@ func buildComponent[N any, O any](comp int, key string, orig O, c chan component
// build out a schema
func buildSchema(key lowmodel.KeyReference[string], orig lowmodel.ValueReference[*base.SchemaProxy],
c chan componentResult[*highbase.SchemaProxy]) {
var sch *highbase.SchemaProxy
sch = highbase.NewSchemaProxy(&lowmodel.NodeReference[*base.SchemaProxy]{
var sch *highbase.SchemaProxy = highbase.NewSchemaProxy(&lowmodel.NodeReference[*base.SchemaProxy]{
Value: orig.Value,
ValueNode: orig.ValueNode,
})
Expand Down
Loading
Loading