github.com/newrelic/newrelic-client-go@v1.1.0/pkg/entities/entity.go (about)

     1  package entities
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  
     7  	log "github.com/sirupsen/logrus"
     8  )
     9  
    10  // Need Outlines to also implement Entity
    11  func (x AlertableEntityOutline) ImplementsEntity()                       {}
    12  func (x ApmApplicationEntityOutline) ImplementsEntity()                  {}
    13  func (x ApmBrowserApplicationEntityOutline) ImplementsEntity()           {}
    14  func (x ApmDatabaseInstanceEntityOutline) ImplementsEntity()             {}
    15  func (x ApmExternalServiceEntityOutline) ImplementsEntity()              {}
    16  func (x BrowserApplicationEntityOutline) ImplementsEntity()              {}
    17  func (x DashboardEntityOutline) ImplementsEntity()                       {}
    18  func (x EntityOutline) ImplementsEntity()                                {}
    19  func (x GenericEntityOutline) ImplementsEntity()                         {}
    20  func (x GenericInfrastructureEntityOutline) ImplementsEntity()           {}
    21  func (x InfrastructureAwsLambdaFunctionEntityOutline) ImplementsEntity() {}
    22  func (x InfrastructureHostEntityOutline) ImplementsEntity()              {}
    23  func (x InfrastructureIntegrationEntityOutline) ImplementsEntity()       {}
    24  func (x MobileApplicationEntityOutline) ImplementsEntity()               {}
    25  func (x SecureCredentialEntityOutline) ImplementsEntity()                {}
    26  func (x SyntheticMonitorEntityOutline) ImplementsEntity()                {}
    27  func (x ThirdPartyServiceEntityOutline) ImplementsEntity()               {}
    28  func (x UnavailableEntityOutline) ImplementsEntity()                     {}
    29  func (x WorkloadEntityOutline) ImplementsEntity()                        {}
    30  
    31  // UnmarshalJSON is used to unmarshal Actor into the correct
    32  // interfaces per field.
    33  func (a *Actor) UnmarshalJSON(b []byte) error {
    34  	var objMap map[string]*json.RawMessage
    35  	err := json.Unmarshal(b, &objMap)
    36  	if err != nil {
    37  		return err
    38  	}
    39  
    40  	for k, v := range objMap {
    41  		if v == nil {
    42  			continue
    43  		}
    44  
    45  		switch k {
    46  		case "entity":
    47  			var e *EntityInterface
    48  			e, err = UnmarshalEntityInterface([]byte(*v))
    49  			if err != nil {
    50  				return err
    51  			}
    52  
    53  			a.Entity = *e
    54  		case "entities":
    55  			var rawEntities []*json.RawMessage
    56  			err = json.Unmarshal(*v, &rawEntities)
    57  			if err != nil {
    58  				return err
    59  			}
    60  
    61  			for _, m := range rawEntities {
    62  				var e *EntityInterface
    63  				e, err = UnmarshalEntityInterface(*m)
    64  				if err != nil {
    65  					return err
    66  				}
    67  
    68  				if e != nil {
    69  					a.Entities = append(a.Entities, *e)
    70  				}
    71  			}
    72  		case "entitySearch":
    73  			var es EntitySearch
    74  			err = json.Unmarshal(*v, &es)
    75  			if err != nil {
    76  				return err
    77  			}
    78  
    79  			a.EntitySearch = es
    80  		default:
    81  			log.Errorf("Unknown key '%s' value: %s", k, string(*v))
    82  		}
    83  	}
    84  
    85  	return nil
    86  }
    87  
    88  // MarshalJSON returns the JSON encoded version of DashboardWidgetRawConfiguration
    89  // (which is already JSON)
    90  func (d DashboardWidgetRawConfiguration) MarshalJSON() ([]byte, error) {
    91  	if d == nil {
    92  		return []byte("null"), nil
    93  	}
    94  
    95  	return d, nil
    96  }
    97  
    98  // UnmarshalJSON sets *d to a copy of the data, as DashboardWidgetRawConfiguration is
    99  // the raw JSON intentionally.
   100  func (d *DashboardWidgetRawConfiguration) UnmarshalJSON(data []byte) error {
   101  	if d == nil {
   102  		return errors.New("entities.DashboardWidgetRawConfiguration: UnmarshalJSON on nil pointer")
   103  	}
   104  
   105  	*d = append((*d)[0:0], data...)
   106  	return nil
   107  }