11package models
22
33import (
4+ "bytes"
5+ "encoding/json"
46 "fmt"
7+ "strings"
8+ "text/tabwriter"
59
610 "github.com/onflow/flow-go-sdk"
711)
@@ -13,6 +17,89 @@ type Account struct {
1317}
1418
1519// String serializer for the account
16- func (a * Account ) String () string {
17- return fmt .Sprintf ("address: %s, balance: %d" , a .Address .String (), a .Balance )
20+ func (a * Account ) String (filter string ) string {
21+ switch filter {
22+ case "" :
23+ return a .stringAll ()
24+ case "balance" :
25+ return fmt .Sprintf ("%d" , a .Balance )
26+ case "code" :
27+ return fmt .Sprintf ("%s" , a .Code )
28+ case "address" :
29+ return fmt .Sprintf ("%s" , a .Address )
30+ case "keys" :
31+ var b bytes.Buffer
32+ writer := tabwriter .NewWriter (& b , 0 , 8 , 1 , '\t' , tabwriter .AlignRight )
33+ a .stringKeys (writer )
34+ writer .Flush ()
35+ return b .String ()
36+ default :
37+ // todo error not valid filter option
38+ return ""
39+ }
40+ }
41+
42+ // serialize complete account to string
43+ func (a * Account ) stringAll () string {
44+ // buffer to write to instead of io - eaiser testing and independed from medium
45+ var b bytes.Buffer
46+ writer := tabwriter .NewWriter (& b , 0 , 8 , 1 , '\t' , tabwriter .AlignRight )
47+
48+ fmt .Fprintf (writer , "\n " )
49+ fmt .Fprintf (writer , "Address\t %s\n " , a .Address )
50+ fmt .Fprintf (writer , "Balance\t %d\n " , a .Balance )
51+
52+ fmt .Fprintf (writer , "Keys\t %d\n " , len (a .Keys ))
53+ a .stringKeys (writer )
54+
55+ fmt .Fprintf (writer , "\n Code\t \t %s" , strings .ReplaceAll (string (a .Code ), "\n " , "\n \t " ))
56+ fmt .Fprintf (writer , "\n " )
57+
58+ writer .Flush ()
59+
60+ return b .String ()
61+ }
62+
63+ func (a * Account ) stringKeys (writer * tabwriter.Writer ) {
64+ for i , key := range a .Keys {
65+ fmt .Fprintf (writer , "\n Key %d\t Public Key\t %x\n " , i , key .PublicKey .Encode ())
66+ fmt .Fprintf (writer , "\t Weight\t %d\n " , key .Weight )
67+ fmt .Fprintf (writer , "\t Signature Algorithm\t %s\n " , key .SigAlgo )
68+ fmt .Fprintf (writer , "\t Hash Algorithm\t %s\n " , key .HashAlgo )
69+ fmt .Fprintf (writer , "\n " )
70+ }
71+ }
72+
73+ // JSON serializer for account
74+ func (a * Account ) JSON (filter string ) string {
75+ var account []byte
76+ var err error
77+
78+ switch filter {
79+ case "" :
80+ account , err = json .Marshal (a )
81+ case "balance" :
82+ account , err = json .Marshal (
83+ struct { Balance uint64 }{Balance : a .Balance },
84+ )
85+ case "code" :
86+ account , err = json .Marshal (
87+ struct { Code []byte }{Code : a .Code },
88+ )
89+ case "address" :
90+ account , err = json .Marshal (
91+ struct { Address flow.Address }{Address : a .Address },
92+ )
93+ case "keys" :
94+ account , err = json .Marshal (a .Keys )
95+ default :
96+ // todo error not valid filter option
97+ return ""
98+ }
99+
100+ if err != nil {
101+ fmt .Println ("error serializing account" )
102+ }
103+
104+ return string (account )
18105}
0 commit comments