github.com/m3db/m3@v1.5.0/src/dbnode/x/m3em/convert/convert.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package convert
    22  
    23  import (
    24  	"fmt"
    25  
    26  	m3emnode "github.com/m3db/m3/src/dbnode/x/m3em/node"
    27  	"github.com/m3db/m3/src/m3em/node"
    28  )
    29  
    30  // AsNodes returns casts a slice of ServiceNodes into m3emnode.Nodes
    31  func AsNodes(nodes []node.ServiceNode) ([]m3emnode.Node, error) {
    32  	m3emnodes := make([]m3emnode.Node, 0, len(nodes))
    33  	for _, n := range nodes {
    34  		mn, ok := n.(m3emnode.Node)
    35  		if !ok {
    36  			return nil, fmt.Errorf("unable to cast: %v to m3dbnode", n.String())
    37  		}
    38  		m3emnodes = append(m3emnodes, mn)
    39  	}
    40  	return m3emnodes, nil
    41  }
    42  
    43  // AsServiceNodes returns casts a slice m3emnode.Nodes into ServiceNodes
    44  func AsServiceNodes(nodes []m3emnode.Node) ([]node.ServiceNode, error) {
    45  	serviceNodes := make([]node.ServiceNode, 0, len(nodes))
    46  	for _, mn := range nodes {
    47  		n, ok := mn.(node.ServiceNode)
    48  		if !ok {
    49  			return nil, fmt.Errorf("unable to cast: %v to ServiceNode", n.String())
    50  		}
    51  		serviceNodes = append(serviceNodes, n)
    52  	}
    53  	return serviceNodes, nil
    54  }