mirror of
				https://github.com/TomWright/dasel.git
				synced 2022-05-22 02:32:45 +03:00 
			
		
		
		
	Add "NewFromFile"
This commit is contained in:
		
							
								
								
									
										32
									
								
								node.go
									
									
									
									
									
								
							
							
						
						
									
										32
									
								
								node.go
									
									
									
									
									
								
							| @@ -2,9 +2,10 @@ package dasel | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"github.com/tomwright/dasel/internal/storage" | ||||
| 	"reflect" | ||||
| 	"regexp" | ||||
|  | ||||
| 	"github.com/tomwright/dasel/internal/storage" | ||||
| ) | ||||
|  | ||||
| // Selector represents the selector for a node. | ||||
| @@ -129,6 +130,35 @@ func New(value interface{}) *Node { | ||||
| 	return rootNode | ||||
| } | ||||
|  | ||||
| // NewFromFile returns a new root node by parsing file. | ||||
| func NewFromFile(path, parser string) (*Node, error) { | ||||
| 	readParser, err := storage.NewReadParserFromString(parser) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| 	data, err := storage.LoadFromFile(path, readParser) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| 	rootNode := &Node{ | ||||
| 		Previous:     nil, | ||||
| 		Next:         nil, | ||||
| 		NextMultiple: nil, | ||||
| 		Selector: Selector{ | ||||
| 			Raw:       ".", | ||||
| 			Current:   ".", | ||||
| 			Remaining: "", | ||||
| 			Type:      "ROOT", | ||||
| 			Property:  "", | ||||
| 		}, | ||||
| 	} | ||||
|  | ||||
| 	rootNode.setRealValue(data) | ||||
| 	return rootNode, nil | ||||
| } | ||||
|  | ||||
| func (n *Node) setValue(newValue interface{}) { | ||||
| 	n.Value = reflect.ValueOf(newValue) | ||||
| 	if n.Selector.Type == "ROOT" { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 beatcracker
					beatcracker