@@ -529,6 +529,11 @@ func jsonMap(v interface{}) interface{} {
529529 return v
530530}
531531
532+ // Skip unmarshaling if there is no value in provider and default is empty.
533+ func shouldSkip (value * Value , def string ) bool {
534+ return ! value .HasValue () && def == ""
535+ }
536+
532537// tryUnmarshallers checks if the value's type implements either one of standard
533538// interfaces in order:
534539// 1. `json.Unmarshaler`
@@ -548,10 +553,15 @@ func (d *decoder) tryUnmarshalers(key string, value reflect.Value, def string) (
548553 switch t := value .Addr ().Interface ().(type ) {
549554 case json.Unmarshaler :
550555 // Skip unmarshaling if there is no value.
551- if ! v . HasValue ( ) {
556+ if shouldSkip ( & v , def ) {
552557 return true , nil
553558 }
554559
560+ // Use default if a value wasn't found.
561+ if ! v .HasValue () {
562+ return true , errorWithKey (t .UnmarshalJSON ([]byte (def )), key )
563+ }
564+
555565 // Marshal the value first.
556566 b , err := json .Marshal (jsonMap (v .Value ()))
557567 if err != nil {
@@ -561,18 +571,26 @@ func (d *decoder) tryUnmarshalers(key string, value reflect.Value, def string) (
561571 // Unmarshal corresponding json.
562572 return true , errorWithKey (t .UnmarshalJSON (b ), key )
563573 case encoding.TextUnmarshaler :
564- // check if we need to use custom defaults
574+ if shouldSkip (& v , def ) {
575+ return true , nil
576+ }
577+
578+ // Use default if a value wasn't found.
565579 if v .HasValue () {
566580 def = v .String ()
567581 }
568582
569583 return true , errorWithKey (t .UnmarshalText ([]byte (def )), key )
570584 case yaml.Unmarshaler :
571- // Skip unmarshaling if there is no value.
572- if ! v .HasValue () {
585+ if shouldSkip (& v , def ) {
573586 return true , nil
574587 }
575588
589+ // Use default if a value wasn't found.
590+ if ! v .HasValue () {
591+ return true , errorWithKey (yaml .Unmarshal ([]byte (def ), value .Addr ().Interface ()), key )
592+ }
593+
576594 b , err := yaml .Marshal (v .Value ())
577595 if err != nil {
578596 return true , err
0 commit comments