Skip to content

Commit

Permalink
Use govet (#3)
Browse files Browse the repository at this point in the history
* use go vet

* Use go vet and fieldalignment
  • Loading branch information
SVilgelm authored Dec 24, 2021
1 parent c1382d8 commit 97eef1f
Show file tree
Hide file tree
Showing 20 changed files with 472 additions and 432 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ jobs:
with:
stable: false
go-version: ${{ env.GO }}
- name: gofmt
run: diff -u <(echo -n) <(gofmt -l . )
- name: go vet
run: go vet ./...
- name: fieldalignment
run: go vet -vettool=$(which fieldalignment) ./...
- name: Run Unit Tests
run: go test -race -cover -coverprofile=coverage.out -covermode=atomic ./...
- name: Codecov
Expand Down
10 changes: 7 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@ ifeq ($(shell uname), Darwin)
all: brew-install
endif

# +lint
all: tidy test
all: go-install tidy lint test

brew-install:
@brew bundle --file $(BREWFILE)

go-install:
@go install golang.org/x/tools/go/analysis/passes/fieldalignment/cmd/fieldalignment@latest

run-test:
@go test -cover -race ./...

test: run-test

# @golangci-lint run --fix ./...
lint:
@golangci-lint run --fix ./...
@go vet ./...
@go vet -vettool=$(which fieldalignment) ./...

tidy:
@go mod tidy
Expand Down
2 changes: 1 addition & 1 deletion spec/bool_or_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
// It MUST be used as a pointer,
// otherwise the `false` can be omitted by json or yaml encoders in case of `omitempty` tag is set.
type BoolOrSchema struct {
Allowed bool
Schema *RefOrSpec[Schema]
Allowed bool
}

// NewBoolOrSchema creates BoolOrSchema object.
Expand Down
58 changes: 31 additions & 27 deletions spec/bool_or_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
)

type testAD struct {
Name string `json:"name,omitempty" yaml:"name,omitempty"`
AP *spec.BoolOrSchema `json:"ap,omitempty" yaml:"ap,omitempty"`
Name string `json:"name,omitempty" yaml:"name,omitempty"`
}

func TestAdditionalPropertiesJSON(t *testing.T) {
Expand Down Expand Up @@ -51,33 +51,37 @@ func TestAdditionalPropertiesJSON(t *testing.T) {
},
} {
t.Run(tt.name, func(t *testing.T) {
var j testAD
require.NoError(t, json.Unmarshal([]byte(tt.data), &j))
require.Equal(t, "foo", j.Name)
if tt.nilAP {
require.Nil(t, j.AP)
} else {
require.NotNil(t, j.AP)
require.Equal(t, tt.allowed, j.AP.Allowed)
require.Equal(t, tt.nilSchema, j.AP.Schema == nil)
}
newJson, err := json.Marshal(&j)
require.NoError(t, err)
require.JSONEq(t, tt.data, string(newJson))
t.Run("json", func(t *testing.T) {
var v testAD
require.NoError(t, json.Unmarshal([]byte(tt.data), &v))
require.Equal(t, "foo", v.Name)
if tt.nilAP {
require.Nil(t, v.AP)
} else {
require.NotNil(t, v.AP)
require.Equal(t, tt.allowed, v.AP.Allowed)
require.Equal(t, tt.nilSchema, v.AP.Schema == nil)
}
newJson, err := json.Marshal(&v)
require.NoError(t, err)
require.JSONEq(t, tt.data, string(newJson))
})

var y testAD
require.NoError(t, yaml.Unmarshal([]byte(tt.data), &y))
require.Equal(t, "foo", y.Name)
if tt.nilAP {
require.Nil(t, y.AP)
} else {
require.NotNil(t, y.AP)
require.Equal(t, tt.allowed, y.AP.Allowed)
require.Equal(t, tt.nilSchema, y.AP.Schema == nil)
}
newYaml, err := yaml.Marshal(&y)
require.NoError(t, err)
require.YAMLEq(t, tt.data, string(newYaml))
t.Run("yaml", func(t *testing.T) {
var v testAD
require.NoError(t, yaml.Unmarshal([]byte(tt.data), &v))
require.Equal(t, "foo", v.Name)
if tt.nilAP {
require.Nil(t, v.AP)
} else {
require.NotNil(t, v.AP)
require.Equal(t, tt.allowed, v.AP.Allowed)
require.Equal(t, tt.nilSchema, v.AP.Schema == nil)
}
newYaml, err := yaml.Marshal(&v)
require.NoError(t, err)
require.YAMLEq(t, tt.data, string(newYaml))
})
})
}

Expand Down
4 changes: 2 additions & 2 deletions spec/discriminator.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ package spec
// dog: '#/components/schemas/Dog'
// monster: 'https://gigantic-server.com/schemas/Monster/schema.json'
type Discriminator struct {
// An object to hold mappings between payload values and schema names or references.
Mapping map[string]string `json:"mapping,omitempty" yaml:"mapping,omitempty"`
// REQUIRED.
// The name of the property in the payload that will hold the discriminator value.
PropertyName string `json:"propertyName" yaml:"propertyName"`
// An object to hold mappings between payload values and schema names or references.
Mapping map[string]string `json:"mapping,omitempty" yaml:"mapping,omitempty"`
}

// NewDiscriminator creates Discriminator object.
Expand Down
60 changes: 30 additions & 30 deletions spec/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,28 @@ package spec
//
// All fields are copied from Parameter Object as is, except name and in fields.
type Header struct {
// Example of the parameter’s potential value.
// The example SHOULD match the specified schema and encoding properties if present.
// The example field is mutually exclusive of the examples field.
// Furthermore, if referencing a schema that contains an example, the example value SHALL override the example provided by the schema.
// To represent examples of media types that cannot naturally be represented in JSON or YAML,
// a string value can contain the example with escaping where necessary.
Example any `json:"example,omitempty" yaml:"example,omitempty"`
// The schema defining the type used for the parameter.
Schema *RefOrSpec[Schema] `json:"schema,omitempty" yaml:"schema,omitempty"`
// Examples of the parameter’s potential value.
// Each example SHOULD contain a value in the correct format as specified in the parameter encoding.
// The examples field is mutually exclusive of the example field.
// Furthermore, if referencing a schema that contains an example, the examples value SHALL override the example provided by the schema.
Examples map[string]*RefOrSpec[Extendable[Example]] `json:"examples,omitempty" yaml:"examples,omitempty"`
// A map containing the representations for the parameter.
// The key is the media type and the value describes it.
// The map MUST only contain one entry.
Content map[string]*Extendable[MediaType] `json:"content,omitempty" yaml:"content,omitempty"`
// A brief description of the header.
// This could contain examples of use.
// CommonMark syntax MAY be used for rich text representation.
Description string `json:"description,omitempty" yaml:"description,omitempty"`
// Determines whether this header is mandatory.
// The property MAY be included and its default value is false.
Required bool `json:"required,omitempty" yaml:"required,omitempty"`
// Specifies that a header is deprecated and SHOULD be transitioned out of usage.
// Default value is false.
Deprecated bool `json:"deprecated,omitempty" yaml:"deprecated,omitempty"`
// Sets the ability to pass empty-valued headers.
// This is valid only for query parameters and allows sending a parameter with an empty value.
// Default value is false.
// If style is used, and if behavior is n/a (cannot be serialized), the value of allowEmptyValue SHALL be ignored.
// Use of this property is NOT RECOMMENDED, as it is likely to be removed in a later revision.
AllowEmptyValue bool `json:"allowEmptyValue,omitempty" yaml:"allowEmptyValue,omitempty"`
// Describes how the parameter value will be serialized depending on the type of the parameter value.
// Default values (based on value of in):
// for query - form;
Expand All @@ -46,24 +52,18 @@ type Header struct {
// This property only applies to parameters with an in value of query.
// The default value is false.
AllowReserved bool `json:"allowReserved,omitempty" yaml:"allowReserved,omitempty"`
// The schema defining the type used for the parameter.
Schema *RefOrSpec[Schema] `json:"schema,omitempty" yaml:"schema,omitempty"`
// Example of the parameter’s potential value.
// The example SHOULD match the specified schema and encoding properties if present.
// The example field is mutually exclusive of the examples field.
// Furthermore, if referencing a schema that contains an example, the example value SHALL override the example provided by the schema.
// To represent examples of media types that cannot naturally be represented in JSON or YAML,
// a string value can contain the example with escaping where necessary.
Example any `json:"example,omitempty" yaml:"example,omitempty"`
// Examples of the parameter’s potential value.
// Each example SHOULD contain a value in the correct format as specified in the parameter encoding.
// The examples field is mutually exclusive of the example field.
// Furthermore, if referencing a schema that contains an example, the examples value SHALL override the example provided by the schema.
Examples map[string]*RefOrSpec[Extendable[Example]] `json:"examples,omitempty" yaml:"examples,omitempty"`
// A map containing the representations for the parameter.
// The key is the media type and the value describes it.
// The map MUST only contain one entry.
Content map[string]*Extendable[MediaType] `json:"content,omitempty" yaml:"content,omitempty"`
// Determines whether this header is mandatory.
// The property MAY be included and its default value is false.
Required bool `json:"required,omitempty" yaml:"required,omitempty"`
// Specifies that a header is deprecated and SHOULD be transitioned out of usage.
// Default value is false.
Deprecated bool `json:"deprecated,omitempty" yaml:"deprecated,omitempty"`
// Sets the ability to pass empty-valued headers.
// This is valid only for query parameters and allows sending a parameter with an empty value.
// Default value is false.
// If style is used, and if behavior is n/a (cannot be serialized), the value of allowEmptyValue SHALL be ignored.
// Use of this property is NOT RECOMMENDED, as it is likely to be removed in a later revision.
AllowEmptyValue bool `json:"allowEmptyValue,omitempty" yaml:"allowEmptyValue,omitempty"`
}

// NewHeaderSpec creates Header object.
Expand Down
Loading

0 comments on commit 97eef1f

Please sign in to comment.