Skip to content
Snippets Groups Projects
request.go 1.89 KiB
Newer Older
  • Learn to ignore specific revisions
  • package main
    
    import (
    	"context"
    	"fmt"
    
    	"encoding/json"
    	"io/ioutil"
    
    	driver "github.com/arangodb/go-driver"
    	"github.com/arangodb/go-driver/http"
    )
    
    // Document with Empty struct to retrieve all data from the DB Document
    type Document map[string]interface{}
    
    // GeneralFormat is a generalformat to send data the frontend
    type GeneralFormat map[string]interface{}
    
    //attr interface{}
    
    //map[1 , 2 , 3 map [ .. ]]
    
    func main() {
    	conn, err := http.NewConnection(http.ConnectionConfig{
    		Endpoints: []string{"http://localhost:8529"},
    	})
    	if err != nil {
    		fmt.Printf("auwa das tut weh") // Handle error
    	}
    	c, err := driver.NewClient(driver.ClientConfig{
    		Connection:     conn,
    		Authentication: driver.BasicAuthentication("root", "openSesame"),
    	})
    	if err != nil {
    		fmt.Printf("doet het niet") // Handle error
    	}
    
    	ctx := context.Background()
    	db, err := c.Database(ctx, "_system")
    	if err != nil {
    		// handle error
    	}
    
    	query := "FOR d IN Characters LIMIT 10 RETURN d"
    	cursor, err := db.Query(ctx, query, nil)
    
    	if err != nil {
    		fmt.Printf("shalom") // handle error
    	}
    	defer cursor.Close()
    	for {
    		var doc Document
    		_, err := cursor.ReadDocument(ctx, &doc)
    		if driver.IsNoMoreDocuments(err) {
    			break
    		} else if err != nil {
    			// handle other errors
    		}
    		//fmt.Printf("%T\n", doc)
    		formatToJSON(doc)
    	}
    
    }
    
    func formatToJSON(doc Document) {
    	//b, err := json.Marshal(doc)
    	//if err != nil {
    	//handle error
    	//}
    
    	data := parseData(doc)
    	fmt.Println(data)
    	file, _ := json.MarshalIndent(data, "", " ")
    
    	_ = ioutil.WriteFile("test.json", file, 0644)
    }
    
    func parseData(doc Document) Document {
    	data := make(Document)
    	data["_id"] = doc["_id"]
    	delete(doc, "_id")
    	data["_key"] = doc["_key"]
    	delete(doc, "_key")
    	data["_rev"] = doc["_rev"]
    	delete(doc, "_rev")
    	data["attributes"] = doc
    
    	//delete(doc, "_key")
    	//data.rev = fmt.Sprintf("%v", doc["_rev"])
    
    	// delete(doc, "_rev")
    	// data.attr = doc
    	return data
    }