-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean up marshaller added hex support
- Loading branch information
Showing
10 changed files
with
60 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package jsonencoder | ||
|
||
import ( | ||
"encoding/hex" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/jhump/protoreflect/dynamic" | ||
|
||
"github.com/mr-tron/base58" | ||
|
||
"github.com/go-json-experiment/json" | ||
"github.com/go-json-experiment/json/jsontext" | ||
"google.golang.org/protobuf/types/known/anypb" | ||
) | ||
|
||
func (e *Encoder) anypb(encoder *jsontext.Encoder, t *anypb.Any, options json.Options) error { | ||
msg, err := e.protoRegistry.Unmarshall(t) | ||
if err != nil { | ||
return fmt.Errorf("unmarshalling proto any: %w", err) | ||
} | ||
setBytesEncoder(t.TypeUrl) | ||
cnt, err := json.Marshal(msg, json.WithMarshalers(json.NewMarshalers(e.marshallers...))) | ||
if err != nil { | ||
return fmt.Errorf("json marshalling proto any: %w", err) | ||
} | ||
return encoder.WriteValue(cnt) | ||
} | ||
|
||
func (e *Encoder) base58Bytes(encoder *jsontext.Encoder, t []byte, options json.Options) error { | ||
return encoder.WriteToken(jsontext.String(base58.Encode(t))) | ||
} | ||
|
||
func (e *Encoder) hexBytes(encoder *jsontext.Encoder, t []byte, options json.Options) error { | ||
return encoder.WriteToken(jsontext.String(hex.EncodeToString(t))) | ||
} | ||
|
||
func setBytesEncoder(typeURL string) { | ||
if strings.Contains(typeURL, "solana") { | ||
dynamic.SetDefaultBytesRepresentation(dynamic.BytesAsBase58) | ||
return | ||
} else { | ||
dynamic.SetDefaultBytesRepresentation(dynamic.BytesAsHex) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters