Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add name list command #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 64 additions & 12 deletions names.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,32 @@ import (
// of a NameShowAsync RPC invocation (or an applicable error).
type FutureNameShowResult chan *rpcclient.Response

// FutureNameListResult is a future promise to deliver the result
// of a NameListAsync RPC invocation (or an applicable error).
type FutureNameListResult chan *rpcclient.Response

func decode(nameShow *ncbtcjson.NameShowResult) error {
if nameShow.NameEncoding == ncbtcjson.Hex {
var nameBytes []byte
nameBytes, err := hex.DecodeString(nameShow.Name)
if err != nil {
return err
}
nameShow.Name = string(nameBytes)
}

if nameShow.ValueEncoding == ncbtcjson.Hex {
var valueBytes []byte
valueBytes, err := hex.DecodeString(nameShow.Value)
if err != nil {
return err
}
nameShow.Value = string(valueBytes)
}

return nil
}

// Receive waits for the Response promised by the future and returns detailed
// information about a name.
func (r FutureNameShowResult) Receive() (*ncbtcjson.NameShowResult, error) {
Expand All @@ -37,24 +63,37 @@ func (r FutureNameShowResult) Receive() (*ncbtcjson.NameShowResult, error) {
return nil, err
}

if nameShow.NameEncoding == ncbtcjson.Hex {
var nameBytes []byte
nameBytes, err = hex.DecodeString(nameShow.Name)
if err != nil {
return nil, err
}
nameShow.Name = string(nameBytes)
err = decode(&nameShow)
if err != nil {
return nil, err
}
if nameShow.ValueEncoding == ncbtcjson.Hex {
var valueBytes []byte
valueBytes, err = hex.DecodeString(nameShow.Value)

return &nameShow, nil
}

// Receive waits for the Response promised by the future and returns detailed
// information about a name.
func (r FutureNameListResult) Receive() (ncbtcjson.NameListResult, error) {
res, err := rpcclient.ReceiveFuture(r)
if err != nil {
return nil, err
}

// Unmarshal result as a name_list result object
var nameList ncbtcjson.NameListResult
err = json.Unmarshal(res, &nameList)
if err != nil {
return nil, err
}

for i := range nameList {
err = decode(&nameList[i])
if err != nil {
return nil, err
}
nameShow.Value = string(valueBytes)
}

return &nameShow, nil
return nameList, nil
}

// NameShowAsync returns an instance of a type that can be used to get the
Expand All @@ -75,6 +114,19 @@ func (c *Client) NameShow(name string, options *ncbtcjson.NameShowOptions) (*ncb
return c.NameShowAsync(name, options).Receive()
}

// NameListAsync returns an instance of a type that can be used to get the
// result of the RPC at some future time by invoking the Receive function on
// the returned instance.
func (c *Client) NameListAsync(name string) FutureNameListResult {
cmd := ncbtcjson.NewNameListCmd(name)
return c.SendCmd(cmd)
}

// NameList returns a list of names.
func (c *Client) NameList(name string) (ncbtcjson.NameListResult, error) {
return c.NameListAsync(name).Receive()
}

// FutureNameScanResult is a future promise to deliver the result
// of a NameScanAsync RPC invocation (or an applicable error).
type FutureNameScanResult chan *rpcclient.Response
Expand Down