github.com/mmatczuk/gohan@v0.0.0-20170206152520-30e45d9bdb69/extension/multienv.go (about)

     1  // Copyright (C) 2016  Juniper Networks, Inc.
     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
    12  // implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package extension
    17  
    18  import "github.com/cloudwan/gohan/schema"
    19  
    20  //MultiEnvironment can handle multiple environment
    21  type MultiEnvironment struct {
    22  	childEnv []Environment
    23  }
    24  
    25  //NewEnvironment create new gohan extension environment based on context
    26  func NewEnvironment(envs []Environment) *MultiEnvironment {
    27  	env := &MultiEnvironment{
    28  		childEnv: envs,
    29  	}
    30  	env.SetUp()
    31  	return env
    32  }
    33  
    34  //SetUp initialize environment
    35  func (env *MultiEnvironment) SetUp() {
    36  }
    37  
    38  //LoadExtensionsForPath for returns extensions for specific path
    39  func (env *MultiEnvironment) LoadExtensionsForPath(extensions []*schema.Extension, path string) error {
    40  	for _, env := range env.childEnv {
    41  		err := env.LoadExtensionsForPath(extensions, path)
    42  		if err != nil {
    43  			return err
    44  		}
    45  	}
    46  	return nil
    47  }
    48  
    49  //HandleEvent handles event
    50  func (env *MultiEnvironment) HandleEvent(event string, context map[string]interface{}) error {
    51  	for _, env := range env.childEnv {
    52  		err := env.HandleEvent(event, context)
    53  		if err != nil {
    54  			return err
    55  		}
    56  	}
    57  	return nil
    58  }
    59  
    60  //Clone makes clone of the environment
    61  func (env *MultiEnvironment) Clone() Environment {
    62  	newEnv := NewEnvironment([]Environment{})
    63  	for _, env := range env.childEnv {
    64  		newEnv.childEnv = append(newEnv.childEnv, env.Clone())
    65  	}
    66  	return newEnv
    67  }