Skip to content

Commit

Permalink
fix: adjust linter rules (#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
nrwiersma authored Nov 26, 2022
1 parent d2b2257 commit e8b1e97
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 47 deletions.
6 changes: 4 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ linters-settings:
linters:
enable-all: true
disable:
- sqlclosecheck # not relevant (SQL)
- rowserrcheck # not relevant (SQL)
- execinquery # not relevant (SQL)
- interfacer # deprecated
- scopelint # deprecated
- maligned # deprecated
Expand All @@ -19,7 +22,7 @@ linters:
- nosnakecase # deprecated
- structcheck # deprecated
- varcheck # deprecated
- cyclop
- cyclop # duplicate of gocyclo
- exhaustive
- exhaustruct
- forcetypeassert
Expand All @@ -32,7 +35,6 @@ linters:
- gomnd
- ireturn
- nestif
- nilnil
- nlreturn
- nonamedreturns
- tagliatelle
Expand Down
9 changes: 2 additions & 7 deletions codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,9 @@ import (
)

var (
timeRType uintptr
ratRType uintptr
)

func init() {
timeRType = reflect2.TypeOf(time.Time{}).RType()
ratRType = reflect2.TypeOf(big.Rat{}).RType()
}
ratRType = reflect2.TypeOf(big.Rat{}).RType()
)

type null struct{}

Expand Down
58 changes: 28 additions & 30 deletions codec_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,45 +140,43 @@ func encoderOfStruct(cfg *frozenConfig, schema Schema, typ reflect2.Type) ValEnc
fields := make([]*structFieldEncoder, 0, len(rec.Fields()))
for _, field := range rec.Fields() {
sf := structDesc.Fields.Get(field.Name())
if sf != nil {
fields = append(fields, &structFieldEncoder{
field: sf.Field,
encoder: encoderOfType(cfg, field.Type(), sf.Field[len(sf.Field)-1].Type()),
})
continue
}

if sf == nil {
if !field.HasDefault() {
// In all other cases, this is a required field
return &errorEncoder{err: fmt.Errorf("avro: record %s is missing required field %q", rec.FullName(), field.Name())}
}

def := field.Default()
if field.Default() == nil {
if field.Type().Type() == Null {
// We write nothing in a Null case, just skip it
continue
}
if !field.HasDefault() {
// In all other cases, this is a required field
err := fmt.Errorf("avro: record %s is missing required field %q", rec.FullName(), field.Name())
return &errorEncoder{err: err}
}

if field.Type().Type() == Union && field.Type().(*UnionSchema).Nullable() {
defaultType := reflect2.TypeOf(&def)
fields = append(fields, &structFieldEncoder{
defaultPtr: reflect2.PtrOf(&def),
encoder: encoderOfPtrUnion(cfg, field.Type(), defaultType),
})
continue
}
def := field.Default()
if field.Default() == nil {
if field.Type().Type() == Null {
// We write nothing in a Null case, just skip it
continue
}

defaultType := reflect2.TypeOf(def)
fields = append(fields, &structFieldEncoder{
defaultPtr: reflect2.PtrOf(def),
encoder: encoderOfType(cfg, field.Type(), defaultType),
})

continue
if field.Type().Type() == Union && field.Type().(*UnionSchema).Nullable() {
defaultType := reflect2.TypeOf(&def)
fields = append(fields, &structFieldEncoder{
defaultPtr: reflect2.PtrOf(&def),
encoder: encoderOfPtrUnion(cfg, field.Type(), defaultType),
})
continue
}
}

defaultType := reflect2.TypeOf(def)
fields = append(fields, &structFieldEncoder{
field: sf.Field,
encoder: encoderOfType(cfg, field.Type(), sf.Field[len(sf.Field)-1].Type()),
defaultPtr: reflect2.PtrOf(def),
encoder: encoderOfType(cfg, field.Type(), defaultType),
})
}

return &structEncoder{typ: typ, fields: fields}
}

Expand Down
9 changes: 1 addition & 8 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ var NoDefault = noDef{}

// NewField creates a new field instance.
func NewField(name string, typ Schema, opts ...SchemaOption) (*Field, error) {
var cfg schemaConfig
cfg := schemaConfig{def: NoDefault}
for _, opt := range opts {
opt(&cfg)
}
Expand Down Expand Up @@ -1257,13 +1257,6 @@ func validateName(name string) error {
}

func validateDefault(name string, schema Schema, def interface{}) (interface{}, error) {
if def == nil {
if schema.Type() != Null && !(schema.Type() == Union && schema.(*UnionSchema).Nullable()) {
// This is an empty default value.
return nil, nil
}
}

def, ok := isValidDefault(schema, def)
if !ok {
return nil, fmt.Errorf("avro: invalid default for field %s. %+v not a %s", name, def, schema.Type())
Expand Down

0 comments on commit e8b1e97

Please sign in to comment.