github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/engine/access/ping/node_info.go (about)

     1  package ping
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"io"
     7  	"os"
     8  
     9  	"github.com/onflow/flow-go/model/flow"
    10  )
    11  
    12  func readExtraNodeInfoJSON(jsonFileName string) (map[flow.Identifier]string, error) {
    13  
    14  	// read the file
    15  	byteValue, err := openAndReadFile(jsonFileName)
    16  	if err != nil {
    17  		return nil, err
    18  	}
    19  
    20  	// unmarshal json data
    21  	result, err := unmarshalNodeInfoFromJSONData(byteValue)
    22  	if err != nil {
    23  		return nil, fmt.Errorf("failed to unmarshal %s: %w", jsonFileName, err)
    24  	}
    25  	return result, nil
    26  }
    27  
    28  func openAndReadFile(fileName string) ([]byte, error) {
    29  	//open
    30  	jsonFile, err := os.Open(fileName)
    31  	if err != nil {
    32  		return nil, fmt.Errorf("failed to open %s: %w", fileName, err)
    33  	}
    34  	defer jsonFile.Close()
    35  
    36  	// read
    37  	byteValue, err := io.ReadAll(jsonFile)
    38  	if err != nil {
    39  		return nil, fmt.Errorf("failed to read %s: %w", fileName, err)
    40  	}
    41  	return byteValue, nil
    42  }
    43  
    44  func unmarshalNodeInfoFromJSONData(jsonData []byte) (map[flow.Identifier]string, error) {
    45  	var result map[flow.Identifier]string
    46  	err := json.Unmarshal(jsonData, &result)
    47  	if err != nil {
    48  		return nil, err
    49  	}
    50  	return result, nil
    51  }