@@ -35,10 +35,7 @@ func UnmarshalJSONStrict(data []byte, v any) error {
3535 }
3636 jsonDecoder := json .NewDecoder (bytes .NewReader (data ))
3737 jsonDecoder .DisallowUnknownFields ()
38- if err := jsonDecoder .Decode (v ); err != nil {
39- return fmt .Errorf ("could not unmarshal as JSON: %v" , err )
40- }
41- return nil
38+ return jsonDecoder .Decode (v )
4239}
4340
4441// UnmarshalYAMLStrict unmarshals the data as YAML, returning a user error on failure.
@@ -49,10 +46,7 @@ func UnmarshalYAMLStrict(data []byte, v any) error {
4946 return nil
5047 }
5148 yamlDecoder := NewYAMLDecoderStrict (bytes .NewReader (data ))
52- if err := yamlDecoder .Decode (v ); err != nil {
53- return fmt .Errorf ("could not unmarshal as YAML: %v" , err )
54- }
55- return nil
49+ return updateYAMLTypeError (yamlDecoder .Decode (v ))
5650}
5751
5852// UnmarshalJSONOrYAMLStrict unmarshals the data as JSON or YAML in order, returning
@@ -79,10 +73,7 @@ func UnmarshalJSONNonStrict(data []byte, v any) error {
7973 return nil
8074 }
8175 jsonDecoder := json .NewDecoder (bytes .NewReader (data ))
82- if err := jsonDecoder .Decode (v ); err != nil {
83- return fmt .Errorf ("could not unmarshal as JSON: %v" , err )
84- }
85- return nil
76+ return jsonDecoder .Decode (v )
8677}
8778
8879// UnmarshalYAMLNonStrict unmarshals the data as YAML, returning a user error on failure.
@@ -93,10 +84,7 @@ func UnmarshalYAMLNonStrict(data []byte, v any) error {
9384 return nil
9485 }
9586 yamlDecoder := NewYAMLDecoderNonStrict (bytes .NewReader (data ))
96- if err := yamlDecoder .Decode (v ); err != nil {
97- return fmt .Errorf ("could not unmarshal as YAML: %v" , err )
98- }
99- return nil
87+ return updateYAMLTypeError (yamlDecoder .Decode (v ))
10088}
10189
10290// UnmarshalJSONOrYAMLNonStrict unmarshals the data as JSON or YAML in order, returning
@@ -200,3 +188,34 @@ func InterfaceSliceOrStringToStringSlice(in any) ([]string, error) {
200188 return nil , fmt .Errorf ("could not interpret %T as string or string slice" , in )
201189 }
202190}
191+
192+ // *** PRIVATE ***
193+
194+ func updateYAMLTypeError (err error ) error {
195+ if err == nil {
196+ return nil
197+ }
198+ var yamlTypeError * yaml.TypeError
199+ if errors .As (err , & yamlTypeError ) {
200+ for i , errString := range yamlTypeError .Errors {
201+ yamlTypeError .Errors [i ] = replaceAfter (
202+ replaceAfter (
203+ errString ,
204+ "already set in type" ,
205+ "already set" ,
206+ ),
207+ "not found in type" ,
208+ "not found" ,
209+ )
210+ }
211+ return yamlTypeError
212+ }
213+ return err
214+ }
215+
216+ func replaceAfter (s string , substitute string , replace string ) string {
217+ if index := strings .Index (s , substitute ); index != - 1 {
218+ return s [:index ] + replace
219+ }
220+ return s
221+ }
0 commit comments