Browse Source

Merge pull request #281 from houshengbo/fix-incorrect-line-number

Print the correct line number of YAML file if an unexpected field is found
Roger Peppe 8 years ago
parent
commit
c95af922ea
2 changed files with 2 additions and 2 deletions
  1. 1 1
      decode.go
  2. 1 1
      decode_test.go

+ 1 - 1
decode.go

@@ -641,7 +641,7 @@ func (d *decoder) mappingStruct(n *node, out reflect.Value) (good bool) {
 			d.unmarshal(n.children[i+1], value)
 			inlineMap.SetMapIndex(name, value)
 		} else if d.strict {
-			d.terrors = append(d.terrors, fmt.Sprintf("line %d: field %s not found in struct %s", n.line+1, name.String(), out.Type()))
+			d.terrors = append(d.terrors, fmt.Sprintf("line %d: field %s not found in struct %s", ni.line+1, name.String(), out.Type()))
 		}
 	}
 	return true

+ 1 - 1
decode_test.go

@@ -999,7 +999,7 @@ func (s *S) TestUnmarshalStrict(c *C) {
 	err = yaml.Unmarshal([]byte("a: 1\nb: 2\nc: 3"), &v)
 	c.Check(err, IsNil)
 	err = yaml.UnmarshalStrict([]byte("a: 1\nb: 2\nc: 3"), &v)
-	c.Check(err, ErrorMatches, "yaml: unmarshal errors:\n  line 1: field c not found in struct struct { A int; B int }")
+	c.Check(err, ErrorMatches, "yaml: unmarshal errors:\n  line 3: field c not found in struct struct { A int; B int }")
 }
 
 //var data []byte