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

     1  package graphql
     2  
     3  import (
     4  	"encoding/json"
     5  	"io"
     6  	"strconv"
     7  
     8  	"github.com/kyma-incubator/compass/components/director/pkg/apperrors"
     9  
    10  	"github.com/kyma-incubator/compass/components/director/pkg/log"
    11  	"github.com/kyma-incubator/compass/components/director/pkg/scalar"
    12  )
    13  
    14  // JSON missing godoc
    15  type JSON string
    16  
    17  // UnmarshalGQL missing godoc
    18  func (j *JSON) UnmarshalGQL(v interface{}) error {
    19  	val, err := scalar.ConvertToString(v)
    20  	if err != nil {
    21  		return err
    22  	}
    23  
    24  	err = json.Unmarshal([]byte(val), new(interface{}))
    25  	if err != nil {
    26  		return apperrors.NewInternalError("JSON input is not a valid JSON")
    27  	}
    28  
    29  	*j = JSON(val)
    30  	return nil
    31  }
    32  
    33  // MarshalGQL missing godoc
    34  func (j JSON) MarshalGQL(w io.Writer) {
    35  	_, err := io.WriteString(w, strconv.Quote(string(j)))
    36  	if err != nil {
    37  		log.D().Errorf("while writing %T: %s", j, err)
    38  	}
    39  }