github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/fee_data.go (about)

     1  package hedera
     2  
     3  /*-
     4   *
     5   * Hedera Go SDK
     6   *
     7   * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC
     8   *
     9   * Licensed under the Apache License, Version 2.0 (the "License");
    10   * you may not use this file except in compliance with the License.
    11   * You may obtain a copy of the License at
    12   *
    13   *      http://www.apache.org/licenses/LICENSE-2.0
    14   *
    15   * Unless required by applicable law or agreed to in writing, software
    16   * distributed under the License is distributed on an "AS IS" BASIS,
    17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    18   * See the License for the specific language governing permissions and
    19   * limitations under the License.
    20   *
    21   */
    22  
    23  import (
    24  	"fmt"
    25  
    26  	"github.com/hashgraph/hedera-protobufs-go/services"
    27  	protobuf "google.golang.org/protobuf/proto"
    28  )
    29  
    30  type FeeData struct {
    31  	NodeData    *FeeComponents
    32  	NetworkData *FeeComponents
    33  	ServiceData *FeeComponents
    34  }
    35  
    36  func _FeeDataFromProtobuf(feeData *services.FeeData) (FeeData, error) {
    37  	if feeData == nil {
    38  		return FeeData{}, errParameterNull
    39  	}
    40  
    41  	nodeData, err := _FeeComponentsFromProtobuf(feeData.Nodedata)
    42  	if err != nil {
    43  		return FeeData{}, err
    44  	}
    45  
    46  	networkData, err := _FeeComponentsFromProtobuf(feeData.Networkdata)
    47  	if err != nil {
    48  		return FeeData{}, err
    49  	}
    50  
    51  	serviceData, err := _FeeComponentsFromProtobuf(feeData.Servicedata)
    52  	if err != nil {
    53  		return FeeData{}, err
    54  	}
    55  
    56  	return FeeData{
    57  		NodeData:    &nodeData,
    58  		NetworkData: &networkData,
    59  		ServiceData: &serviceData,
    60  	}, nil
    61  }
    62  
    63  func (feeData FeeData) _ToProtobuf() *services.FeeData {
    64  	var nodeData *services.FeeComponents
    65  	if feeData.NodeData != nil {
    66  		nodeData = feeData.NodeData._ToProtobuf()
    67  	}
    68  
    69  	var networkData *services.FeeComponents
    70  	if feeData.NetworkData != nil {
    71  		networkData = feeData.NetworkData._ToProtobuf()
    72  	}
    73  
    74  	var serviceData *services.FeeComponents
    75  	if feeData.ServiceData != nil {
    76  		serviceData = feeData.ServiceData._ToProtobuf()
    77  	}
    78  
    79  	return &services.FeeData{
    80  		Nodedata:    nodeData,
    81  		Networkdata: networkData,
    82  		Servicedata: serviceData,
    83  	}
    84  }
    85  
    86  // ToBytes returns the byte representation of the FeeData
    87  func (feeData FeeData) ToBytes() []byte {
    88  	data, err := protobuf.Marshal(feeData._ToProtobuf())
    89  	if err != nil {
    90  		return make([]byte, 0)
    91  	}
    92  
    93  	return data
    94  }
    95  
    96  // FeeDataFromBytes returns a FeeData struct from a raw byte array
    97  func FeeDataFromBytes(data []byte) (FeeData, error) {
    98  	if data == nil {
    99  		return FeeData{}, errByteArrayNull
   100  	}
   101  	pb := services.FeeData{}
   102  	err := protobuf.Unmarshal(data, &pb)
   103  	if err != nil {
   104  		return FeeData{}, err
   105  	}
   106  
   107  	info, err := _FeeDataFromProtobuf(&pb)
   108  	if err != nil {
   109  		return FeeData{}, err
   110  	}
   111  
   112  	return info, nil
   113  }
   114  
   115  // String returns a string representation of the FeeData
   116  func (feeData FeeData) String() string {
   117  	return fmt.Sprintf("\nNodedata: %s\nNetworkdata: %s\nServicedata: %s\n", feeData.NodeData.String(), feeData.NetworkData.String(), feeData.ServiceData.String())
   118  }