summaryrefslogtreecommitdiffstats
path: root/vendor/maunium.net/go/mautrix/util/marshal.go
diff options
context:
space:
mode:
authormsglm <msglm@techchud.xyz>2023-10-27 07:08:25 -0500
committermsglm <msglm@techchud.xyz>2023-10-27 07:08:25 -0500
commit032a7e0c1188d3507b8d9a9571f2446a43cf775b (patch)
tree2bd38c01bc7761a6195e426082ce7191ebc765a1 /vendor/maunium.net/go/mautrix/util/marshal.go
parent56e7bd01ca09ad52b0c4f48f146a20a4f1b78696 (diff)
downloadmatterbridge-msglm-032a7e0c1188d3507b8d9a9571f2446a43cf775b.tar.gz
matterbridge-msglm-032a7e0c1188d3507b8d9a9571f2446a43cf775b.tar.bz2
matterbridge-msglm-032a7e0c1188d3507b8d9a9571f2446a43cf775b.zip
apply https://github.com/42wim/matterbridge/pull/1864v1.26.0+0.1.0
Diffstat (limited to 'vendor/maunium.net/go/mautrix/util/marshal.go')
-rw-r--r--vendor/maunium.net/go/mautrix/util/marshal.go30
1 files changed, 30 insertions, 0 deletions
diff --git a/vendor/maunium.net/go/mautrix/util/marshal.go b/vendor/maunium.net/go/mautrix/util/marshal.go
new file mode 100644
index 00000000..180adef2
--- /dev/null
+++ b/vendor/maunium.net/go/mautrix/util/marshal.go
@@ -0,0 +1,30 @@
+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
+}