...

Source file src/edge-infra.dev/pkg/edge/auth-proxy/utils/utils.go

Documentation: edge-infra.dev/pkg/edge/auth-proxy/utils

     1  package utils
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/99designs/gqlgen/client"
     9  	"github.com/mitchellh/mapstructure"
    10  )
    11  
    12  type Response struct {
    13  	Data       interface{}            `json:"data"`
    14  	Errors     json.RawMessage        `json:"errors"`
    15  	Extensions map[string]interface{} `json:"extensions"`
    16  }
    17  
    18  func FilterEdgeRoles(roles []string) []string {
    19  	edgeRoles := make([]string, 0)
    20  	for _, role := range roles {
    21  		if strings.HasPrefix(role, "EDGE") {
    22  			edgeRoles = append(edgeRoles, role)
    23  		}
    24  	}
    25  	return edgeRoles
    26  }
    27  
    28  func GetGraphqlResponse(body []byte) (*Response, error) {
    29  	respDataRaw := &Response{}
    30  	if body != nil {
    31  		if err := json.Unmarshal(body, &respDataRaw); err != nil {
    32  			return nil, fmt.Errorf("failed parse response body to json: %w", err)
    33  		}
    34  	}
    35  	return respDataRaw, nil
    36  }
    37  
    38  func GetGraphqlRequest(body []byte) (*client.Request, error) {
    39  	respDataRaw := &client.Request{}
    40  	if body != nil {
    41  		if err := json.Unmarshal(body, &respDataRaw); err != nil {
    42  			return nil, fmt.Errorf("failed parse request body to json: %w", err)
    43  		}
    44  	}
    45  	return respDataRaw, nil
    46  }
    47  
    48  func Unpack(data interface{}, into interface{}) error {
    49  	d, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
    50  		Result:      into,
    51  		TagName:     "json",
    52  		ErrorUnused: false,
    53  		ZeroFields:  true,
    54  	})
    55  	if err != nil {
    56  		return fmt.Errorf("mapstructure: %s", err.Error())
    57  	}
    58  	return d.Decode(data)
    59  }
    60  

View as plain text