github.com/binkynet/BinkyNet@v1.12.1-0.20240421190447-da4e34c20be0/apis/v1/local_worker_config_helper.go (about)

     1  // Copyright 2018 Ewout Prangsma
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // Author Ewout Prangsma
    16  //
    17  
    18  package v1
    19  
    20  // DeviceByID returns the device with given ID.
    21  // Return false if not found.
    22  func (c LocalWorkerConfig) DeviceByID(id DeviceID) (*Device, bool) {
    23  	for _, dev := range c.GetDevices() {
    24  		if dev.Id == id {
    25  			return dev, true
    26  		}
    27  	}
    28  	return nil, false
    29  }
    30  
    31  // ObjectByID returns the object with given ID.
    32  // Return false if not found.
    33  func (c LocalWorkerConfig) ObjectByID(id ObjectID) (*Object, bool) {
    34  	for _, obj := range c.GetObjects() {
    35  		if obj.Id == id {
    36  			return obj, true
    37  		}
    38  	}
    39  	return nil, false
    40  }
    41  
    42  // Validate the given configuration, returning nil on ok,
    43  // or an error upon validation issues.
    44  func (c LocalWorkerConfig) Validate() error {
    45  	for _, d := range c.Devices {
    46  		if err := d.Validate(); err != nil {
    47  			return err
    48  		}
    49  	}
    50  	for _, o := range c.Objects {
    51  		if err := o.Validate(); err != nil {
    52  			return err
    53  		}
    54  		for _, conn := range o.Connections {
    55  			connName := conn.Key
    56  			for pinIdx, p := range conn.Pins {
    57  				if _, found := c.DeviceByID(p.DeviceId); !found {
    58  					return InvalidArgument("Device '%s' not found at index %d of connection '%s' in object '%s'", p.DeviceId, pinIdx, connName, o.Id)
    59  				}
    60  			}
    61  
    62  		}
    63  	}
    64  	return nil
    65  }