binary/xml: replace XMLString with standard String method

Closes #1170
This commit is contained in:
Tulir Asokan
2026-06-11 12:43:07 +03:00
parent 3706a7d315
commit 0ff50824f6
11 changed files with 32 additions and 32 deletions
+2 -2
View File
@@ -565,9 +565,9 @@ func (cli *Client) sendAppState(ctx context.Context, patch appstate.PatchInfo, a
if respCollectionAttr.OptionalString("type") == "error" {
errorTag, ok := respCollection.GetOptionalChildByTag("error")
mainErr := fmt.Errorf("%w: %s", ErrAppStateUpdate, respCollection.XMLString())
mainErr := fmt.Errorf("%w: %s", ErrAppStateUpdate, &respCollection)
if ok {
mainErr = fmt.Errorf("%w (%s): %s", ErrAppStateUpdate, patch.Type, errorTag.XMLString())
mainErr = fmt.Errorf("%w (%s): %s", ErrAppStateUpdate, patch.Type, &errorTag)
}
if ok && errorTag.AttrGetter().Int("code") == 409 && allowRetry {
zerolog.Ctx(ctx).Warn().Err(mainErr).Msg("Failed to update app state, trying to apply conflicts and retry")
+4 -4
View File
@@ -15,14 +15,14 @@ import (
"unicode/utf8"
)
// Options to control how Node.XMLString behaves.
// Options to control how Node.String behaves.
var (
IndentXML = false
MaxBytesToPrintAsHex = 128
)
// XMLString converts the Node to its XML representation
func (n *Node) XMLString() string {
// String converts the Node to its XML representation
func (n Node) String() string {
content := n.contentString()
if len(content) == 0 {
return fmt.Sprintf("<%[1]s%[2]s/>", n.Tag, n.attributeString())
@@ -66,7 +66,7 @@ func (n *Node) contentString() []string {
switch content := n.Content.(type) {
case []Node:
for _, item := range content {
split = append(split, strings.Split(item.XMLString(), "\n")...)
split = append(split, strings.Split(item.String(), "\n")...)
}
case []byte:
if strContent := printable(content); len(strContent) > 0 {
+5 -5
View File
@@ -827,7 +827,7 @@ func (cli *Client) handleFrame(ctx context.Context, data []byte) {
cli.Log.Debugf("Errored frame hex: %s", hex.EncodeToString(decompressed))
return
}
cli.recvLog.Debugf("%s", node.XMLString())
cli.recvLog.Debugf("%s", node)
if node.Tag == "xmlstreamend" {
if !cli.isExpectedDisconnect() {
cli.Log.Warnf("Received stream end frame")
@@ -868,7 +868,7 @@ Loop:
duration := time.Since(start)
close(doneChan)
if duration > 5*time.Second {
cli.Log.Warnf("Node handling took %s for %s", duration, node.XMLString())
cli.Log.Warnf("Node handling took %s for %s", duration, node)
}
}()
ticker.Reset(30 * time.Second)
@@ -878,10 +878,10 @@ Loop:
ticker.Stop()
continue Loop
case <-ticker.C:
cli.Log.Warnf("Node handling is taking long for %s (started %s ago)", node.XMLString(), time.Since(start))
cli.Log.Warnf("Node handling is taking long for %s (started %s ago)", node, time.Since(start))
}
}
cli.Log.Warnf("Continuing handling of %s in background as it's taking too long", node.XMLString())
cli.Log.Warnf("Continuing handling of %s in background as it's taking too long", node)
ticker.Stop()
case <-connCtx.Done():
cli.Log.Debugf("Closing handler queue loop")
@@ -906,7 +906,7 @@ func (cli *Client) sendNodeAndGetData(ctx context.Context, node waBinary.Node) (
return nil, fmt.Errorf("failed to marshal node: %w", err)
}
cli.sendLog.Debugf("%s", node.XMLString())
cli.sendLog.Debugf("%s", &node)
return payload, sock.SendFrame(ctx, payload)
}
+3 -3
View File
@@ -64,7 +64,7 @@ func (cli *Client) handleStreamError(ctx context.Context, node *waBinary.Node) {
go cli.dispatchEvent(&events.CATRefreshError{Error: err})
}
default:
cli.Log.Errorf("Unknown stream error: %s", node.XMLString())
cli.Log.Errorf("Unknown stream error: %s", node)
go cli.dispatchEvent(&events.StreamError{Code: code, Raw: node})
}
}
@@ -131,7 +131,7 @@ func (cli *Client) handleConnectFailure(ctx context.Context, node *waBinary.Node
cli.Log.Warnf("Failed to delete store after %d failure: %v", int(reason), err)
}
} else if reason == events.ConnectFailureTempBanned {
cli.Log.Warnf("Temporary ban connect failure: %s", node.XMLString())
cli.Log.Warnf("Temporary ban connect failure: %s", node)
go cli.dispatchEvent(&events.TemporaryBan{
Code: events.TempBanReason(ag.Int("code")),
Expire: time.Duration(ag.Int("expire")) * time.Second,
@@ -150,7 +150,7 @@ func (cli *Client) handleConnectFailure(ctx context.Context, node *waBinary.Node
} else if willAutoReconnect {
cli.Log.Warnf("Got %d/%s connect failure, assuming automatic reconnect will handle it", int(reason), message)
} else {
cli.Log.Warnf("Unknown connect failure: %s", node.XMLString())
cli.Log.Warnf("Unknown connect failure: %s", node)
go cli.dispatchEvent(&events.ConnectFailure{Reason: reason, Message: message, Raw: node})
}
}
+3 -3
View File
@@ -214,9 +214,9 @@ func parseIQError(node *waBinary.Node) error {
func (iqe *IQError) Error() string {
if iqe.Code == 0 {
if iqe.ErrorNode != nil {
return fmt.Sprintf("info query returned unknown error: %s", iqe.ErrorNode.XMLString())
return fmt.Sprintf("info query returned unknown error: %s", iqe.ErrorNode)
} else if iqe.RawNode != nil {
return fmt.Sprintf("info query returned unexpected response: %s", iqe.RawNode.XMLString())
return fmt.Sprintf("info query returned unexpected response: %s", iqe.RawNode)
} else {
return "unknown info query error"
}
@@ -231,7 +231,7 @@ func (iqe *IQError) Is(other error) bool {
} else if iqe.Code != 0 && otherIQE.Code != 0 {
return otherIQE.Code == iqe.Code && otherIQE.Text == iqe.Text
} else if iqe.ErrorNode != nil && otherIQE.ErrorNode != nil {
return iqe.ErrorNode.XMLString() == otherIQE.ErrorNode.XMLString()
return iqe.ErrorNode.String() == otherIQE.ErrorNode.String()
} else {
return false
}
+3 -3
View File
@@ -523,7 +523,7 @@ func (cli *Client) GetJoinedGroups(ctx context.Context) ([]*types.GroupInfo, err
var allRedactedPhones []store.RedactedPhoneEntry
for _, child := range children {
if child.Tag != "group" {
cli.Log.Debugf("Unexpected child in group list response: %s", child.XMLString())
cli.Log.Debugf("Unexpected child in group list response: %s", &child)
continue
}
parsed, parseErr := cli.parseGroupNode(&child)
@@ -760,7 +760,7 @@ func (cli *Client) parseGroupNode(groupNode *waBinary.Node) (*types.GroupInfo, e
case "suspended":
group.Suspended = true
default:
cli.Log.Debugf("Unknown element in group node %s: %s", group.JID.String(), child.XMLString())
cli.Log.Debugf("Unknown element in group node %s: %s", group.JID.String(), &child)
}
if !childAG.OK() {
cli.Log.Warnf("Possibly failed to parse %s element in group node: %+v", child.Tag, childAG.Errors)
@@ -890,7 +890,7 @@ func (cli *Client) parseGroupChange(node *waBinary.Node) (*events.GroupInfo, []s
topicChild := child.GetChildByTag("body")
topicBytes, ok := topicChild.Content.([]byte)
if !ok {
return nil, nil, fmt.Errorf("group change description has unexpected body: %s", topicChild.XMLString())
return nil, nil, fmt.Errorf("group change description has unexpected body: %s", &topicChild)
}
topicStr = string(topicBytes)
}
+1 -1
View File
@@ -82,7 +82,7 @@ func (cli *Client) queryMediaConn(ctx context.Context) (*MediaConn, error) {
}
for _, child := range respMC.GetChildren() {
if child.Tag != "host" {
cli.Log.Warnf("Unexpected child in media_conn element: %s", child.XMLString())
cli.Log.Warnf("Unexpected child in media_conn element: %s", &child)
continue
}
cag := child.AttrGetter()
+5 -5
View File
@@ -30,15 +30,15 @@ func (cli *Client) handleEncryptNotification(ctx context.Context, node *waBinary
ag := count.AttrGetter()
otksLeft := ag.Int("value")
if !ag.OK() {
cli.Log.Warnf("Didn't get number of OTKs left in encryption notification %s", node.XMLString())
cli.Log.Warnf("Didn't get number of OTKs left in encryption notification %s", node)
return
}
cli.Log.Infof("Got prekey count from server: %s", node.XMLString())
cli.Log.Infof("Got prekey count from server: %s", node)
if otksLeft < MinPreKeyCount {
cli.uploadPreKeys(ctx, false)
}
} else if _, ok := node.GetOptionalChildByTag("identity"); ok {
cli.Log.Debugf("Got identity change for %s: %s, deleting all identities/sessions for that number", from, node.XMLString())
cli.Log.Debugf("Got identity change for %s: %s, deleting all identities/sessions for that number", from, node)
err := cli.Store.Identities.DeleteAllIdentities(ctx, from.User)
if err != nil {
cli.Log.Warnf("Failed to delete all identities of %s from store after identity change: %v", from, err)
@@ -66,7 +66,7 @@ func (cli *Client) handleEncryptNotification(ctx context.Context, node *waBinary
}
cli.dispatchEvent(&events.IdentityChange{JID: from, Timestamp: ts})
} else {
cli.Log.Debugf("Got unknown encryption notification from server: %s", node.XMLString())
cli.Log.Debugf("Got unknown encryption notification from server: %s", node)
}
}
@@ -255,7 +255,7 @@ func (cli *Client) handleBlocklist(ctx context.Context, node *waBinary.Node) {
Action: events.BlocklistChangeAction(ag.String("action")),
}
if !ag.OK() {
cli.Log.Warnf("Unexpected data in blocklist event child %v: %v", child.XMLString(), ag.Error())
cli.Log.Warnf("Unexpected data in blocklist event child %s: %v", &child, ag.Error())
continue
}
evt.Changes = append(evt.Changes, change)
+1 -1
View File
@@ -180,7 +180,7 @@ func preKeyToNode(key *keys.PreKey) waBinary.Node {
func nodeToPreKeyBundle(deviceID uint32, node waBinary.Node) (*prekey.Bundle, error) {
errorNode, ok := node.GetOptionalChildByTag("error")
if ok && errorNode.Tag == "error" {
return nil, fmt.Errorf("got error getting prekeys: %s", errorNode.XMLString())
return nil, fmt.Errorf("got error getting prekeys: %s", &errorNode)
}
registrationBytes, ok := node.GetChildByTag("registration").Content.([]byte)
+2 -2
View File
@@ -38,7 +38,7 @@ func (cli *Client) handleGroupedReceipt(partialReceipt events.Receipt, participa
partialReceipt.MessageIDs = []types.MessageID{pag.String("key")}
for _, child := range participants.GetChildren() {
if child.Tag != "user" {
cli.Log.Warnf("Unexpected node in grouped receipt participants: %s", child.XMLString())
cli.Log.Warnf("Unexpected node in grouped receipt participants: %s", &child)
continue
}
ag := child.AttrGetter()
@@ -46,7 +46,7 @@ func (cli *Client) handleGroupedReceipt(partialReceipt events.Receipt, participa
receipt.Timestamp = ag.UnixTime("t")
receipt.MessageSource.Sender = ag.JID("jid")
if !ag.OK() {
cli.Log.Warnf("Failed to parse user node %s in grouped receipt: %v", child.XMLString(), ag.Error())
cli.Log.Warnf("Failed to parse user node %s in grouped receipt: %v", &child, ag.Error())
continue
}
cli.dispatchEvent(&receipt)
+3 -3
View File
@@ -191,11 +191,11 @@ func (cli *Client) retryFrame(
timeout time.Duration,
) (*waBinary.Node, error) {
if isAuthErrorDisconnect(origResp) {
cli.Log.Debugf("%s (%s) was interrupted by websocket disconnection (%s), not retrying as it looks like an auth error", id, reqType, origResp.XMLString())
cli.Log.Debugf("%s (%s) was interrupted by websocket disconnection (%s), not retrying as it looks like an auth error", id, reqType, origResp)
return nil, &DisconnectedError{Action: reqType, Node: origResp}
}
cli.Log.Debugf("%s (%s) was interrupted by websocket disconnection (%s), waiting for reconnect to retry...", id, reqType, origResp.XMLString())
cli.Log.Debugf("%s (%s) was interrupted by websocket disconnection (%s), waiting for reconnect to retry...", id, reqType, origResp)
if !cli.WaitForConnection(5 * time.Second) {
cli.Log.Debugf("Websocket didn't reconnect within 5 seconds of failed %s (%s)", reqType, id)
return nil, &DisconnectedError{Action: reqType, Node: origResp}
@@ -228,7 +228,7 @@ func (cli *Client) retryFrame(
return nil, ErrIQTimedOut
}
if isDisconnectNode(resp) {
cli.Log.Debugf("Retrying %s %s was interrupted by websocket disconnection (%v), not retrying anymore", reqType, id, resp.XMLString())
cli.Log.Debugf("Retrying %s %s was interrupted by websocket disconnection (%s), not retrying anymore", reqType, id, resp)
return nil, &DisconnectedError{Action: fmt.Sprintf("%s (retry)", reqType), Node: resp}
}
return resp, nil