1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- package db
- import (
- "encoding/json"
- "encoding/xml"
- "fmt"
- "strings"
- "unsafe"
- )
- // M is a shortcut for map[string]any
- type M map[string]any
- // NewM new map[string]any
- func NewM() M {
- return make(map[string]any)
- }
- // Set key-value to map[string]any
- func (m M) Set(k string, v any) {
- m[k] = v
- }
- // Get value from map[string]interface{}
- func (m M) Get(k string) (v any, ok bool) {
- v, ok = m[k]
- return
- }
- // String map to json string
- func (m M) String() string {
- bs, _ := json.Marshal(m)
- return *(*string)(unsafe.Pointer(&bs))
- }
- // Bytes map to json bytes
- func (m M) Bytes() []byte {
- bs, _ := json.Marshal(m)
- return bs
- }
- // MarshalXML allows type Map to be used with xml.Marshal.
- func (m M) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
- start.Name = xml.Name{
- Space: "",
- Local: "map",
- }
- if err := e.EncodeToken(start); err != nil {
- return err
- }
- for key, value := range m {
- elem := xml.StartElement{
- Name: xml.Name{Space: "", Local: key},
- Attr: []xml.Attr{},
- }
- if err := e.EncodeElement(value, elem); err != nil {
- return err
- }
- }
- return e.EncodeToken(xml.EndElement{Name: start.Name})
- }
- // Int64ArrayToIn array to db IN string
- func Int64ArrayToIn(arrs []int64) string {
- ss := fmt.Sprint(arrs)
- return strings.Replace(strings.Trim(fmt.Sprint(ss), "[]"), " ", ",", -1)
- }
|