github.com/loomnetwork/gamechain@v0.0.0-20200406110549-36c47eb97a92/tools/battleground_utility/utility.go (about)

     1  package battleground_utility
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/gogo/protobuf/jsonpb"
     6  	"github.com/gogo/protobuf/proto"
     7  	"github.com/loomnetwork/go-loom/common"
     8  	"github.com/loomnetwork/go-loom/types"
     9  	"github.com/pkg/errors"
    10  	"io"
    11  	"io/ioutil"
    12  	"math/big"
    13  	"os"
    14  )
    15  
    16  func ProtoMessageToJsonString(pb proto.Message) (string, error) {
    17  	m := jsonpb.Marshaler{
    18  		OrigName:     false,
    19  		Indent:       "  ",
    20  		EmitDefaults: true,
    21  	}
    22  
    23  	json, err := m.MarshalToString(pb)
    24  	if err != nil {
    25  		return "", fmt.Errorf("error marshaling Proto to JSON: %s", err.Error())
    26  	}
    27  
    28  	return json, nil
    29  }
    30  
    31  func ProtoMessageToJsonStringNoIndent(pb proto.Message) (string, error) {
    32  	m := jsonpb.Marshaler{
    33  		OrigName:     false,
    34  		Indent:       "",
    35  		EmitDefaults: true,
    36  	}
    37  
    38  	json, err := m.MarshalToString(pb)
    39  	if err != nil {
    40  		return "", fmt.Errorf("error marshaling Proto to JSON: %s", err.Error())
    41  	}
    42  
    43  	return json, nil
    44  }
    45  
    46  func PrintProtoMessageAsJson(out io.Writer, pb proto.Message) error {
    47  	m := jsonpb.Marshaler{
    48  		OrigName:     false,
    49  		Indent:       "  ",
    50  		EmitDefaults: true,
    51  	}
    52  
    53  	if err := m.Marshal(out, pb); err != nil {
    54  		return fmt.Errorf("error marshaling Proto to JSON: %s", err.Error())
    55  	}
    56  
    57  	return nil
    58  }
    59  
    60  func PrintProtoMessageAsJsonToStdout(pb proto.Message) error {
    61  	if err := PrintProtoMessageAsJson(os.Stdout, pb); err != nil {
    62  		return err
    63  	}
    64  
    65  	return nil
    66  }
    67  
    68  func ReadJsonFileToProtoMessage(filename string, message proto.Message) error {
    69  	json, err := ReadFileToString(filename)
    70  	if err != nil {
    71  		return errors.Wrap(err, "error reading file"+filename)
    72  	}
    73  
    74  	if err := jsonpb.UnmarshalString(json, message); err != nil {
    75  		return errors.Wrap(err, "error parsing JSON file "+filename)
    76  	}
    77  
    78  	return nil
    79  }
    80  
    81  func ReadJsonStringToProtoMessage(json string, message proto.Message) error {
    82  	if err := jsonpb.UnmarshalString(json, message); err != nil {
    83  		return errors.Wrap(err, "error parsing JSON ")
    84  	}
    85  
    86  	return nil
    87  }
    88  
    89  func ReadFileToString(filename string) (string, error) {
    90  	bytes, err := ioutil.ReadFile(filename)
    91  	if err != nil {
    92  		return "", err
    93  	}
    94  
    95  	return string(bytes), nil
    96  }
    97  
    98  func MarshalBigIntProto(v *big.Int) *types.BigUInt {
    99  	return &types.BigUInt{Value: common.BigUInt{Int: v}}
   100  }