Skip to content

Commit

Permalink
Optimize the field parsing for object display.
Browse files Browse the repository at this point in the history
  • Loading branch information
JaydenLink committed Oct 24, 2023
1 parent fbfcb2a commit 7902e81
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
2 changes: 1 addition & 1 deletion examples/object/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func main() {

func SuiGetObject() {
rsp, err := cli.SuiGetObject(ctx, models.SuiGetObjectRequest{
ObjectId: "0xfe3e114168d65ca9c86e43ce0f8dc4f8e0fa5a03634a4c6bf292679f6d73ec72",
ObjectId: "0xaf647fef62f139f9bbb3ece219d40a49024331531fbe7e5ac3f5718fc1f23c62",
Options: models.SuiObjectDataOptions{
ShowContent: true,
ShowDisplay: true,
Expand Down
53 changes: 52 additions & 1 deletion models/read_object.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package models

import (
"encoding/json"
"github.com/tidwall/gjson"
)

type SuiObjectDataOptions struct {
ShowType bool `json:"showType"`
ShowContent bool `json:"showContent"`
Expand Down Expand Up @@ -103,10 +108,56 @@ type SuiRawMoveObject struct {
}

type DisplayFieldsResponse struct {
Data interface{} `json:"data,omitempty"`
Data any `json:"data,omitempty"`
Error SuiObjectResponseError `json:"error,omitempty"`
}

type Display interface {
Name() string
Description() string
Link() string
ImageURL() string
ThumbnailURL() string
ProjectURL() string
Creator() string
}

func (display DisplayFieldsResponse) Description() string {
return display.value("description")
}

func (display DisplayFieldsResponse) Name() string {
return display.value("name")
}

func (display DisplayFieldsResponse) Link() string {
return display.value("link")
}

func (display DisplayFieldsResponse) ImageURL() string {
return display.value("image_url")
}

func (display DisplayFieldsResponse) ThumbnailURL() string {
return display.value("thumbnail_url")
}

func (display DisplayFieldsResponse) Creator() string {
return display.value("creator")
}

func (display DisplayFieldsResponse) ProjectURL() string {
return display.value("project_url")
}

func (display DisplayFieldsResponse) value(field string) string {
if display.Data == nil || display.Error.Error != "" {
return ""
}
bys, _ := json.Marshal(display.Data)
return gjson.GetBytes(bys, field).String()
}

type SuiParsedData struct {
DataType string `json:"dataType"`
SuiMoveObject
Expand Down

0 comments on commit 7902e81

Please sign in to comment.