github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/worker/uniter/relation/shim.go (about)

     1  // Copyright 2020 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package relation
     5  
     6  import (
     7  	"github.com/juju/names/v5"
     8  
     9  	"github.com/juju/juju/api/agent/uniter"
    10  )
    11  
    12  type stateTrackerStateShim struct {
    13  	*uniter.State
    14  }
    15  
    16  func (s *stateTrackerStateShim) Relation(tag names.RelationTag) (Relation, error) {
    17  	rel, err := s.State.Relation(tag)
    18  	if err != nil {
    19  		return nil, err
    20  	}
    21  	return &relationShim{rel}, nil
    22  }
    23  
    24  // RelationById returns the existing relation with the given id.
    25  func (s *stateTrackerStateShim) RelationById(id int) (Relation, error) {
    26  	rel, err := s.State.RelationById(id)
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  	return &relationShim{rel}, nil
    31  }
    32  
    33  // Unit returns the existing unit with the given tag.
    34  func (s *stateTrackerStateShim) Unit(tag names.UnitTag) (Unit, error) {
    35  	unit, err := s.State.Unit(tag)
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  	return &unitShim{unit}, nil
    40  }
    41  
    42  type relationShim struct {
    43  	*uniter.Relation
    44  }
    45  
    46  func (s *relationShim) Unit(uTag names.UnitTag) (RelationUnit, error) {
    47  	u, err := s.Relation.Unit(uTag)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  	return &RelationUnitShim{u}, nil
    52  }
    53  
    54  type unitShim struct {
    55  	*uniter.Unit
    56  }
    57  
    58  func (s *unitShim) Application() (Application, error) {
    59  	app, err := s.Unit.Application()
    60  	if err != nil {
    61  		return nil, err
    62  	}
    63  	return &applicationShim{app}, nil
    64  }
    65  
    66  type applicationShim struct {
    67  	*uniter.Application
    68  }
    69  
    70  type RelationUnitShim struct {
    71  	*uniter.RelationUnit
    72  }
    73  
    74  func (r *RelationUnitShim) Relation() Relation {
    75  	return &relationShim{r.RelationUnit.Relation()}
    76  }