carta icon indicating copy to clipboard operation
carta copied to clipboard

Does not work for custom data type

Open mannu2612 opened this issue 3 years ago • 2 comments

I have a geometry column in one of my tables and for that I have defined a custom data type with Scan and Value methods like this:

type Point struct {
	X float64
	Y float64
	Z float64
}

type Coordinate Point

// Scan scan value into geom.Point, implements sql.Scanner interface
func (c *Coordinate) Scan(value interface{}) error {

	b, ok := value.([]byte)
	if !ok {
		return errors.New("not a byte" array)
	}
	dst := make([]byte, len(b))
	_, err := hex.Decode(dst, b)
	if err != nil {
		return err
	}
	gt, err := ewkb.Unmarshal(dst)
	if err != nil {
		return err
	}
	c.X = gt.FlatCoords()[0]
	c.Y = gt.FlatCoords()[1]
	return nil
}

// Converting cordinate to geom.point
func (c Coordinate) Value() (driver.Value, error) {
	b := geom.NewPoint(geom.XY)
	b.SetCoords(geom.Coord{c.X, c.Y})
	a, err := wkt.Marshal(b)
	s := fmt.Sprintf("SRID=%d;%s", 4326, a)
	return s, err
}

And this Coordinate type is used in the DB struct like show below:

type Survey struct {
        SurveyID         string    `db:"survey_id"`
	CompanyName      string    `db:"company_name"`
        Gcps []Gcp
}

type Gcp struct {
	GcpID           int64       `db:"gcp_id"`
	PointID string      `db:"point_id"`
	Coordinate      *Coordinate `db:"coordinate"`
}

When I try to map the DB rows to the Survey model, all the fields are mapped but the coordinate is left out and its Scan method is never called.

Following is the code snippet:

	rows, err := i.db.Query(query)
	if err != nil {
		log.Error(err)
		return nil, err
	}
	fs := []model.Survey{}
	err = carta.Map(rows, &fs)
	if err != nil {
		log.Error(err)
		return nil, err
	}

mannu2612 avatar Feb 27 '22 16:02 mannu2612

which database are you using. In addition, what is the underlying data type of the Point type in the database?

jackskj avatar Mar 02 '22 03:03 jackskj

@jackskj I am using postgresql with postgis extension. Data type in DB is geometry

mannu2612 avatar Mar 02 '22 03:03 mannu2612