github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/pkg/graphql/http_headers.go (about)

     1  package graphql
     2  
     3  import (
     4  	"encoding/json"
     5  	"io"
     6  
     7  	"github.com/kyma-incubator/compass/components/director/pkg/log"
     8  
     9  	"github.com/kyma-incubator/compass/components/director/pkg/apperrors"
    10  	"github.com/kyma-incubator/compass/components/director/pkg/scalar"
    11  )
    12  
    13  // HTTPHeaders missing godoc
    14  type HTTPHeaders map[string][]string
    15  
    16  // UnmarshalGQL missing godoc
    17  func (y *HTTPHeaders) UnmarshalGQL(v interface{}) error {
    18  	headers, err := scalar.ConvertToMapStringStringArray(v)
    19  	if err != nil {
    20  		return err
    21  	}
    22  
    23  	*y = headers
    24  
    25  	return nil
    26  }
    27  
    28  // MarshalGQL missing godoc
    29  func (y HTTPHeaders) MarshalGQL(w io.Writer) {
    30  	err := scalar.WriteMarshalled(y, w)
    31  	if err != nil {
    32  		log.D().Printf("while writing %T: %s", y, err)
    33  		return
    34  	}
    35  }
    36  
    37  // HTTPHeadersSerialized missing godoc
    38  type HTTPHeadersSerialized string
    39  
    40  // Unmarshal missing godoc
    41  func (y *HTTPHeadersSerialized) Unmarshal() (map[string][]string, error) {
    42  	var data map[string][]string
    43  	if y == nil {
    44  		return data, nil
    45  	}
    46  
    47  	err := json.Unmarshal([]byte(*y), &data)
    48  	if err != nil {
    49  		return nil, apperrors.NewInvalidDataError("unable to unmarshal HTTP headers: %s", err.Error())
    50  	}
    51  
    52  	return data, nil
    53  }
    54  
    55  // NewHTTPHeadersSerialized missing godoc
    56  func NewHTTPHeadersSerialized(h map[string][]string) (HTTPHeadersSerialized, error) {
    57  	data, err := json.Marshal(h)
    58  	if err != nil {
    59  		return "", apperrors.NewInvalidDataError("unable to marshal HTTP headers: %s", err.Error())
    60  	}
    61  
    62  	return HTTPHeadersSerialized(data), nil
    63  }