@@ -324,32 +324,58 @@ func (dec *tomlDecoder) arrayAppend(context Context, path []interface{}, rhsNode
324324}
325325
326326func (dec * tomlDecoder ) processArrayTable (currentNode * toml.Node ) (bool , error ) {
327- log .Debug ("Entering processArrayTable " )
327+ log .Debug ("c " )
328328 fullPath := dec .getFullPath (currentNode .Child ())
329329 log .Debug ("Fullpath: %v" , fullPath )
330330
331+ c := Context {}
332+ c = c .SingleChildContext (dec .rootMap )
333+
334+ pathToCheck := fullPath
335+ if len (fullPath ) >= 1 {
336+ pathToCheck = fullPath [:len (fullPath )- 1 ]
337+ }
338+
339+ // if fullPath points to an array of maps rather than a map
340+ // then it should set this element into the _last_ element of that array.
341+ // Because TOML. So we'll inject the last index into the path.
342+ readOp := createTraversalTree (pathToCheck , traversePreferences {DontAutoCreate : true }, false )
343+
344+ resultContext , err := dec .d .GetMatchingNodes (c , readOp )
345+ if err != nil {
346+ return false , err
347+ }
348+ if resultContext .MatchingNodes .Len () >= 1 {
349+ match := resultContext .MatchingNodes .Front ().Value .(* CandidateNode )
350+ // path refers to an array, we need to add this to the last element in the array
351+ if match .Kind == SequenceNode {
352+ fullPath = append (pathToCheck , len (match .Content )- 1 , fullPath [len (fullPath )- 1 ])
353+ log .Debugf ("Adding to end of %v array, using path: %v" , pathToCheck , fullPath )
354+ }
355+ }
356+
331357 // need to use the array append exp to add another entry to
332358 // this array: fullpath += [ thing ]
333-
334359 hasValue := dec .parser .NextExpression ()
335- if ! hasValue {
336- return false , fmt .Errorf ("error retrieving table %v value: %w" , fullPath , dec .parser .Error ())
337- }
338360
339361 tableNodeValue := & CandidateNode {
340362 Kind : MappingNode ,
341363 Tag : "!!map" ,
342364 }
343-
344- tableValue := dec .parser .Expression ()
345- runAgainstCurrentExp , err := dec .decodeKeyValuesIntoMap (tableNodeValue , tableValue )
346- log .Debugf ("table node err: %w" , err )
347- if err != nil && ! errors .Is (err , io .EOF ) {
348- return false , err
365+ runAgainstCurrentExp := false
366+ // if the next value is a ArrayTable or Table, then its not part of this declaration (not a key value pair)
367+ // so lets leave that expression for the next round of parsing
368+ if hasValue && (dec .parser .Expression ().Kind == toml .ArrayTable || dec .parser .Expression ().Kind == toml .Table ) {
369+ runAgainstCurrentExp = true
370+ } else if hasValue {
371+ // otherwise, if there is a value, it must be some key value pairs of the
372+ // first object in the array!
373+ tableValue := dec .parser .Expression ()
374+ runAgainstCurrentExp , err = dec .decodeKeyValuesIntoMap (tableNodeValue , tableValue )
375+ if err != nil && ! errors .Is (err , io .EOF ) {
376+ return false , err
377+ }
349378 }
350- c := Context {}
351-
352- c = c .SingleChildContext (dec .rootMap )
353379
354380 // += function
355381 err = dec .arrayAppend (c , fullPath , tableNodeValue )
0 commit comments