github.com/Axway/agent-sdk@v1.1.101/pkg/apic/apiserver/models/management/v1alpha1/APISpecLintingRuleset.go (about)

     1  /*
     2   * This file is automatically generated
     3   */
     4  
     5  package management
     6  
     7  import (
     8  	"encoding/json"
     9  
    10  	apiv1 "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/api/v1"
    11  
    12  	"github.com/Axway/agent-sdk/pkg/util/log"
    13  )
    14  
    15  var (
    16  	APISpecLintingRulesetCtx log.ContextField = "apiSpecLintingRuleset"
    17  
    18  	_APISpecLintingRulesetGVK = apiv1.GroupVersionKind{
    19  		GroupKind: apiv1.GroupKind{
    20  			Group: "management",
    21  			Kind:  "APISpecLintingRuleset",
    22  		},
    23  		APIVersion: "v1alpha1",
    24  	}
    25  
    26  	APISpecLintingRulesetScopes = []string{""}
    27  )
    28  
    29  const (
    30  	APISpecLintingRulesetResourceName            = "apispeclintingrulesets"
    31  	ApiSpecLintingRulesetRevisionSubResourceName = "revision"
    32  )
    33  
    34  func APISpecLintingRulesetGVK() apiv1.GroupVersionKind {
    35  	return _APISpecLintingRulesetGVK
    36  }
    37  
    38  func init() {
    39  	apiv1.RegisterGVK(_APISpecLintingRulesetGVK, APISpecLintingRulesetScopes[0], APISpecLintingRulesetResourceName)
    40  	log.RegisterContextField(APISpecLintingRulesetCtx)
    41  }
    42  
    43  // APISpecLintingRuleset Resource
    44  type APISpecLintingRuleset struct {
    45  	apiv1.ResourceMeta
    46  	Owner    *apiv1.Owner              `json:"owner"`
    47  	Revision interface{}               `json:"revision"`
    48  	Spec     ApiSpecLintingRulesetSpec `json:"spec"`
    49  }
    50  
    51  // NewAPISpecLintingRuleset creates an empty *APISpecLintingRuleset
    52  func NewAPISpecLintingRuleset(name string) *APISpecLintingRuleset {
    53  	return &APISpecLintingRuleset{
    54  		ResourceMeta: apiv1.ResourceMeta{
    55  			Name:             name,
    56  			GroupVersionKind: _APISpecLintingRulesetGVK,
    57  		},
    58  	}
    59  }
    60  
    61  // APISpecLintingRulesetFromInstanceArray converts a []*ResourceInstance to a []*APISpecLintingRuleset
    62  func APISpecLintingRulesetFromInstanceArray(fromArray []*apiv1.ResourceInstance) ([]*APISpecLintingRuleset, error) {
    63  	newArray := make([]*APISpecLintingRuleset, 0)
    64  	for _, item := range fromArray {
    65  		res := &APISpecLintingRuleset{}
    66  		err := res.FromInstance(item)
    67  		if err != nil {
    68  			return make([]*APISpecLintingRuleset, 0), err
    69  		}
    70  		newArray = append(newArray, res)
    71  	}
    72  
    73  	return newArray, nil
    74  }
    75  
    76  // AsInstance converts a APISpecLintingRuleset to a ResourceInstance
    77  func (res *APISpecLintingRuleset) AsInstance() (*apiv1.ResourceInstance, error) {
    78  	meta := res.ResourceMeta
    79  	meta.GroupVersionKind = APISpecLintingRulesetGVK()
    80  	res.ResourceMeta = meta
    81  
    82  	m, err := json.Marshal(res)
    83  	if err != nil {
    84  		return nil, err
    85  	}
    86  
    87  	instance := apiv1.ResourceInstance{}
    88  	err = json.Unmarshal(m, &instance)
    89  	if err != nil {
    90  		return nil, err
    91  	}
    92  
    93  	return &instance, nil
    94  }
    95  
    96  // FromInstance converts a ResourceInstance to a APISpecLintingRuleset
    97  func (res *APISpecLintingRuleset) FromInstance(ri *apiv1.ResourceInstance) error {
    98  	if ri == nil {
    99  		res = nil
   100  		return nil
   101  	}
   102  	var err error
   103  	rawResource := ri.GetRawResource()
   104  	if rawResource == nil {
   105  		rawResource, err = json.Marshal(ri)
   106  		if err != nil {
   107  			return err
   108  		}
   109  	}
   110  	err = json.Unmarshal(rawResource, res)
   111  	return err
   112  }
   113  
   114  // MarshalJSON custom marshaller to handle sub resources
   115  func (res *APISpecLintingRuleset) MarshalJSON() ([]byte, error) {
   116  	m, err := json.Marshal(&res.ResourceMeta)
   117  	if err != nil {
   118  		return nil, err
   119  	}
   120  
   121  	var out map[string]interface{}
   122  	err = json.Unmarshal(m, &out)
   123  	if err != nil {
   124  		return nil, err
   125  	}
   126  
   127  	out["owner"] = res.Owner
   128  	out["revision"] = res.Revision
   129  	out["spec"] = res.Spec
   130  
   131  	return json.Marshal(out)
   132  }
   133  
   134  // UnmarshalJSON custom unmarshaller to handle sub resources
   135  func (res *APISpecLintingRuleset) UnmarshalJSON(data []byte) error {
   136  	var err error
   137  
   138  	aux := &apiv1.ResourceInstance{}
   139  	err = json.Unmarshal(data, aux)
   140  	if err != nil {
   141  		return err
   142  	}
   143  
   144  	res.ResourceMeta = aux.ResourceMeta
   145  	res.Owner = aux.Owner
   146  
   147  	// ResourceInstance holds the spec as a map[string]interface{}.
   148  	// Convert it to bytes, then convert to the spec type for the resource.
   149  	sr, err := json.Marshal(aux.Spec)
   150  	if err != nil {
   151  		return err
   152  	}
   153  
   154  	err = json.Unmarshal(sr, &res.Spec)
   155  	if err != nil {
   156  		return err
   157  	}
   158  
   159  	// marshalling subresource Revision
   160  	if v, ok := aux.SubResources["revision"]; ok {
   161  		sr, err = json.Marshal(v)
   162  		if err != nil {
   163  			return err
   164  		}
   165  
   166  		delete(aux.SubResources, "revision")
   167  		err = json.Unmarshal(sr, &res.Revision)
   168  		if err != nil {
   169  			return err
   170  		}
   171  	}
   172  
   173  	return nil
   174  }
   175  
   176  // PluralName returns the plural name of the resource
   177  func (res *APISpecLintingRuleset) PluralName() string {
   178  	return APISpecLintingRulesetResourceName
   179  }