Skip to content

Commit

Permalink
fix: support IsBool() on MapperValue implementations.
Browse files Browse the repository at this point in the history
Fixes #381
  • Loading branch information
alecthomas committed Sep 30, 2023
1 parent 2af1ea5 commit 46c0384
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

var (
mapperValueType = reflect.TypeOf((*MapperValue)(nil)).Elem()
boolMapperType = reflect.TypeOf((*BoolMapper)(nil)).Elem()
boolMapperValueType = reflect.TypeOf((*BoolMapperValue)(nil)).Elem()
jsonUnmarshalerType = reflect.TypeOf((*json.Unmarshaler)(nil)).Elem()
textUnmarshalerType = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
binaryUnmarshalerType = reflect.TypeOf((*encoding.BinaryUnmarshaler)(nil)).Elem()
Expand Down Expand Up @@ -47,6 +47,12 @@ type MapperValue interface {
Decode(ctx *DecodeContext) error
}

// BoolMapperValue may be implemented by fields in order to provide custom mappings for boolean values.
type BoolMapperValue interface {
MapperValue
IsBool() bool
}

type mapperValueAdapter struct {
isBool bool
}
Expand Down Expand Up @@ -194,7 +200,7 @@ func (r *Registry) ForType(typ reflect.Type) Mapper {
for _, impl := range []reflect.Type{typ, reflect.PtrTo(typ)} {
if impl.Implements(mapperValueType) {
// FIXME: This should pass in the bool mapper.
return &mapperValueAdapter{impl.Implements(boolMapperType)}
return &mapperValueAdapter{impl.Implements(boolMapperValueType)}
}
}
// Next, try explicitly registered types.
Expand Down

0 comments on commit 46c0384

Please sign in to comment.