dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts@v1.0.2/models/subscription.go (about)

     1  //
     2  // Copyright (C) 2020-2021 IOTech Ltd
     3  //
     4  // SPDX-License-Identifier: Apache-2.0
     5  
     6  package models
     7  
     8  import (
     9  	"encoding/json"
    10  
    11  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/errors"
    12  )
    13  
    14  type Subscription struct {
    15  	DBTimestamp
    16  	Categories     []string
    17  	Labels         []string
    18  	Channels       []Address
    19  	Description    string
    20  	Id             string
    21  	Receiver       string
    22  	Name           string
    23  	ResendLimit    int
    24  	ResendInterval string
    25  	AdminState     AdminState
    26  }
    27  
    28  // ChannelType controls the range of values which constitute valid delivery types for channels
    29  type ChannelType string
    30  
    31  func (subscription *Subscription) UnmarshalJSON(b []byte) error {
    32  	var alias struct {
    33  		DBTimestamp
    34  		Categories     []string
    35  		Labels         []string
    36  		Channels       []interface{}
    37  		Description    string
    38  		Id             string
    39  		Receiver       string
    40  		Name           string
    41  		ResendLimit    int
    42  		ResendInterval string
    43  		AdminState     AdminState
    44  	}
    45  	if err := json.Unmarshal(b, &alias); err != nil {
    46  		return errors.NewCommonEdgeX(errors.KindContractInvalid, "Failed to unmarshal intervalAction.", err)
    47  	}
    48  	channels := make([]Address, len(alias.Channels))
    49  	for i, c := range alias.Channels {
    50  		address, err := instantiateAddress(c)
    51  		if err != nil {
    52  			return errors.NewCommonEdgeXWrapper(err)
    53  		}
    54  		channels[i] = address
    55  	}
    56  
    57  	*subscription = Subscription{
    58  		DBTimestamp:    alias.DBTimestamp,
    59  		Categories:     alias.Categories,
    60  		Labels:         alias.Labels,
    61  		Description:    alias.Description,
    62  		Id:             alias.Id,
    63  		Receiver:       alias.Receiver,
    64  		Name:           alias.Name,
    65  		ResendLimit:    alias.ResendLimit,
    66  		ResendInterval: alias.ResendInterval,
    67  		Channels:       channels,
    68  		AdminState:     alias.AdminState,
    69  	}
    70  	return nil
    71  }