mirror of
https://mau.dev/mautrix/go.git
synced 2026-03-14 14:25:53 +01:00
Remove last thing in util package
This commit is contained in:
parent
50ca07046a
commit
11414eaa0b
2 changed files with 24 additions and 35 deletions
29
responses.go
29
responses.go
|
|
@ -3,16 +3,17 @@ package mautrix
|
|||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
"github.com/tidwall/sjson"
|
||||
"go.mau.fi/util/jsontime"
|
||||
|
||||
"maunium.net/go/mautrix/event"
|
||||
"maunium.net/go/mautrix/id"
|
||||
"maunium.net/go/mautrix/util"
|
||||
)
|
||||
|
||||
// RespWhoami is the JSON response for https://spec.matrix.org/v1.2/client-server-api/#get_matrixclientv3accountwhoami
|
||||
|
|
@ -270,8 +271,26 @@ type marshalableRespSync RespSync
|
|||
|
||||
var syncPathsToDelete = []string{"account_data", "presence", "to_device", "device_lists", "device_one_time_keys_count", "rooms"}
|
||||
|
||||
// marshalAndDeleteEmpty marshals a JSON object, then uses gjson to delete empty objects at the given gjson paths.
|
||||
func marshalAndDeleteEmpty(marshalable interface{}, paths []string) ([]byte, error) {
|
||||
data, err := json.Marshal(marshalable)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, path := range paths {
|
||||
res := gjson.GetBytes(data, path)
|
||||
if res.IsObject() && len(res.Raw) == 2 {
|
||||
data, err = sjson.DeleteBytes(data, path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to delete empty %s: %w", path, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (rs *RespSync) MarshalJSON() ([]byte, error) {
|
||||
return util.MarshalAndDeleteEmpty((*marshalableRespSync)(rs), syncPathsToDelete)
|
||||
return marshalAndDeleteEmpty((*marshalableRespSync)(rs), syncPathsToDelete)
|
||||
}
|
||||
|
||||
type DeviceLists struct {
|
||||
|
|
@ -299,7 +318,7 @@ type marshalableSyncLeftRoom SyncLeftRoom
|
|||
var syncLeftRoomPathsToDelete = []string{"summary", "state", "timeline"}
|
||||
|
||||
func (slr SyncLeftRoom) MarshalJSON() ([]byte, error) {
|
||||
return util.MarshalAndDeleteEmpty((marshalableSyncLeftRoom)(slr), syncLeftRoomPathsToDelete)
|
||||
return marshalAndDeleteEmpty((marshalableSyncLeftRoom)(slr), syncLeftRoomPathsToDelete)
|
||||
}
|
||||
|
||||
type SyncJoinedRoom struct {
|
||||
|
|
@ -324,7 +343,7 @@ type marshalableSyncJoinedRoom SyncJoinedRoom
|
|||
var syncJoinedRoomPathsToDelete = []string{"summary", "state", "timeline", "ephemeral", "account_data"}
|
||||
|
||||
func (sjr SyncJoinedRoom) MarshalJSON() ([]byte, error) {
|
||||
return util.MarshalAndDeleteEmpty((marshalableSyncJoinedRoom)(sjr), syncJoinedRoomPathsToDelete)
|
||||
return marshalAndDeleteEmpty((marshalableSyncJoinedRoom)(sjr), syncJoinedRoomPathsToDelete)
|
||||
}
|
||||
|
||||
type SyncInvitedRoom struct {
|
||||
|
|
@ -337,7 +356,7 @@ type marshalableSyncInvitedRoom SyncInvitedRoom
|
|||
var syncInvitedRoomPathsToDelete = []string{"summary"}
|
||||
|
||||
func (sir SyncInvitedRoom) MarshalJSON() ([]byte, error) {
|
||||
return util.MarshalAndDeleteEmpty((marshalableSyncInvitedRoom)(sir), syncInvitedRoomPathsToDelete)
|
||||
return marshalAndDeleteEmpty((marshalableSyncInvitedRoom)(sir), syncInvitedRoomPathsToDelete)
|
||||
}
|
||||
|
||||
type SyncKnockedRoom struct {
|
||||
|
|
|
|||
|
|
@ -1,30 +0,0 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
"github.com/tidwall/sjson"
|
||||
)
|
||||
|
||||
// MarshalAndDeleteEmpty marshals a JSON object, then uses gjson to delete empty objects at the given gjson paths.
|
||||
//
|
||||
// This can be used as a convenient way to create a marshaler that omits empty non-pointer structs.
|
||||
// See mautrix.RespSync for example.
|
||||
func MarshalAndDeleteEmpty(marshalable interface{}, paths []string) ([]byte, error) {
|
||||
data, err := json.Marshal(marshalable)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, path := range paths {
|
||||
res := gjson.GetBytes(data, path)
|
||||
if res.IsObject() && len(res.Raw) == 2 {
|
||||
data, err = sjson.DeleteBytes(data, path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to delete empty %s: %w", path, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue