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

     1  // Copyright 2020-2021 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  import (
    21  	fmt "fmt"
    22  	"strings"
    23  )
    24  
    25  // ObjectAddress is a strongly typed address of an object.
    26  // It contains <module> '/' <object-id>
    27  type ObjectAddress string
    28  
    29  // GlobalModuleID defines the module part of an ObjectAddress for addresses
    30  // that are independent of specific modules.
    31  const GlobalModuleID = "GLOBAL"
    32  
    33  // SplitAddress splits an address string into its components.
    34  func SplitAddress(input ObjectAddress) (module, local string, err error) {
    35  	parts := strings.Split(string(input), "/")
    36  	if len(parts) != 2 {
    37  		return "", "", fmt.Errorf("Expected <module>/<local>")
    38  	}
    39  	return parts[0], parts[1], nil
    40  }
    41  
    42  // JoinModuleLocal joins a module ID with local ID into an address.
    43  func JoinModuleLocal(module, local string) ObjectAddress {
    44  	return ObjectAddress(module + "/" + local)
    45  }
    46  
    47  // IsGlobal returns true if the given address has a global module ID.
    48  func (addr ObjectAddress) IsGlobal() bool {
    49  	module, _, _ := SplitAddress(addr)
    50  	return module == GlobalModuleID
    51  }