argument.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package db
  2. import (
  3. "encoding/json"
  4. "encoding/xml"
  5. "fmt"
  6. "strings"
  7. "unsafe"
  8. )
  9. // M is a shortcut for map[string]any
  10. type M map[string]any
  11. // NewM new map[string]any
  12. func NewM() M {
  13. return make(map[string]any)
  14. }
  15. // Set key-value to map[string]any
  16. func (m M) Set(k string, v any) {
  17. m[k] = v
  18. }
  19. // Get value from map[string]interface{}
  20. func (m M) Get(k string) (v any, ok bool) {
  21. v, ok = m[k]
  22. return
  23. }
  24. // String map to json string
  25. func (m M) String() string {
  26. bs, _ := json.Marshal(m)
  27. return *(*string)(unsafe.Pointer(&bs))
  28. }
  29. // Bytes map to json bytes
  30. func (m M) Bytes() []byte {
  31. bs, _ := json.Marshal(m)
  32. return bs
  33. }
  34. // MarshalXML allows type Map to be used with xml.Marshal.
  35. func (m M) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
  36. start.Name = xml.Name{
  37. Space: "",
  38. Local: "map",
  39. }
  40. if err := e.EncodeToken(start); err != nil {
  41. return err
  42. }
  43. for key, value := range m {
  44. elem := xml.StartElement{
  45. Name: xml.Name{Space: "", Local: key},
  46. Attr: []xml.Attr{},
  47. }
  48. if err := e.EncodeElement(value, elem); err != nil {
  49. return err
  50. }
  51. }
  52. return e.EncodeToken(xml.EndElement{Name: start.Name})
  53. }
  54. // Int64ArrayToIn array to db IN string
  55. func Int64ArrayToIn(arrs []int64) string {
  56. ss := fmt.Sprint(arrs)
  57. return strings.Replace(strings.Trim(fmt.Sprint(ss), "[]"), " ", ",", -1)
  58. }