Hi 

i have a struct  event and inner that i have struct RequestRecord
*type RequestRecord struct {*
* ID string `json:"id"`*
* Addr string `json:"addr"`*
* Host string `json:"host"`*
* Method string `json:"method"`*
* UserAgent string `json:"useragent"`*
*}*
*type event struct {*
* ID string `json:"id"`*
* TimeStamp string `json:"timestamp"`*
* Action string `json:"action"`*
* Length int `json:"length"`*
* Repository string `json:"repository"`*
* FromRepository string `json:"fromRepository"`*
* URL string `json:"url"`*
* Tag string `json:"tag"`*
* Request RequestRecord `json:"request"`*
*}*

in inserting to database  i get this error:
*sql: converting argument $10 type: unsupported type main.RequestRecord, a 
struct*

i attached main.go

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/64d26f28-6ff2-4e5f-9d1c-134a3b838f05n%40googlegroups.com.
package main

import (
	"database/sql"
	"database/sql/driver"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"

	"github.com/gorilla/mux"
	"github.com/lib/pq"
	_ "github.com/mattn/go-sqlite3"
)

type Descriptor struct {
	mediaType  string `json:"mediaType"`
	size       int    `json:"size"`
	digest     string `json:"digest"`
	length     int    `json:"length"`
	repository string `json:"repository"`
	url        string `json:"url"`
	tag        string `json:"tag"`
}

type SourceRecord struct {
	Addr       string `json:"addr"`
	InstanceID string `json:"instanceID"`
}

type ActorRecord struct {
	Name string `json:"name"`
}

type RequestRecord struct {
	ID        string `json:"id"`
	Addr      string `json:"addr"`
	Host      string `json:"host"`
	Method    string `json:"method"`
	UserAgent string `json:"useragent"`
}

type event struct {
	ID             string        `json:"id"`
	TimeStamp      string        `json:"timestamp"`
	Action         string        `json:"action"`
	Length         int           `json:"length"`
	Repository     string        `json:"repository"`
	FromRepository string        `json:"fromRepository"`
	URL            string        `json:"url"`
	Tag            string        `json:"tag"`
	Request        RequestRecord `json:"request"`
	Actor          ActorRecord   `json:"actor"`
	Source         SourceRecord  `json:"source"`
	Target         Descriptor    `json:"target"`
}

func (u RequestRecord) Value() (driver.Value, error) {
	return fmt.Sprintf("(%s,%s,%s,%s,%s)", u.ID, u.Addr, u.Host, u.Method, u.UserAgent), nil
}

var x []string

func InitDB(filepath string) *sql.DB {
	db, err := sql.Open("sqlite3", filepath)
	if err != nil {
		panic(err)
	}
	if db == nil {
		panic("db nil")
	}
	return db
}

func CreateTable(db *sql.DB) {
	// create table if not exists
	sql_table := `
	CREATE TABLE IF NOT EXISTS events(	
		ID TEXT NOT NULL PRIMARY KEY,
		TimeStamp TEXT,
		Action TEXT,
		Length TEXT,
		Repository TEXT,
		FromRepository TEXT,
		URL TEXT,
		Tag TEXT,
		Request TEXT,
		Actor TEXT,
		Source TEXT,
		Target TEXT
	);
	`

	_, err := db.Exec(sql_table)
	if err != nil {
		panic(err)
	}
}

func StoreItem(db *sql.DB, items []event) {
	sql_additem := `
	INSERT INTO events(	
		ID,
		TimeStamp,
		Action,
		Length,
		Repository,
		FromRepository,
		URL,
		Tag,
		Request,
		Actor,
		Source,
		Target
	) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
	`

	stmt, err := db.Prepare(sql_additem)
	if err != nil {
		panic(err)
	}
	defer stmt.Close()

	for _, item := range items {
		_, err2 := stmt.Exec(item.ID, item.TimeStamp, item.Action, item.Length, item.Repository,
			item.FromRepository, item.URL, item.URL, item.Tag, item.Request, item.Actor, item.Source, item.Target)
		if err2 != nil {
			panic(err2)
		}
	}
}

type allEvents []event

var events = allEvents{}

func insertEvent(w http.ResponseWriter, r *http.Request) {
	var newEvent event
	reqBody, err := ioutil.ReadAll(r.Body)
	if err != nil {
		fmt.Fprintf(w, "please insert correct event info")
	}
	json.Unmarshal(reqBody, &newEvent)

	const dbpath = "shahkey.db"

	db := InitDB(dbpath)
	defer db.Close()
	CreateTable(db)

	events = append(events, newEvent)
	StoreItem(db, pq.Array(events), pq.Array(&x))
	w.WriteHeader(http.StatusCreated)
	json.NewEncoder(w).Encode(newEvent)
}

func main() {

	router := mux.NewRouter().StrictSlash(true)

	router.HandleFunc("/events", insertEvent).Methods("POST")
	log.Fatal(http.ListenAndServe(":8080", router))
}

Reply via email to