This is a implementation of dim, which is built in python. But this time in go.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

50 lines
1.1 KiB

package main
import (
"fmt"
"dim/query"
"dim/types"
)
type (
ZoneListOptions struct {
Attributes types.FieldList `json:"attributes"`
}
)
var (
zoneListMap = map[string]string{
"name": "z.name",
"modified_by": "z.modified_by",
"modified_at": "z.modified_at",
"created_by": "z.modified_by",
"created_at": "z.modified_at",
}
)
func zoneList(c *Context, req Request, res *Response) error {
options := ZoneListOptions{
Attributes: types.NewFieldList("name"),
}
if err := req.ParseAtLeast(0, &options); err != nil {
res.AddMessage(LevelError, "could not parse options: %s", err)
return nil
}
selClause := query.FieldListToSelect(options.Attributes, zoneListMap)
rows, err := c.tx.Query(fmt.Sprintf(`select %s from zones z`, selClause))
if err != nil {
res.AddMessage(LevelError, "could not return result")
return fmt.Errorf("could not get zone list: %s", err)
}
defer rows.Close()
res.Result["zones"], err = query.RowsToMap(rows)
if err != nil {
delete(res.Result, "zones")
res.AddMessage(LevelError, "could not return result")
return fmt.Errorf("could not parse zone list: %v", err)
}
return nil
}