github.com/qioalice/ekago/v3@v3.3.2-0.20221202205325-5c262d586ee4/ekaerr/namespace.go (about)

     1  // Copyright © 2020. All rights reserved.
     2  // Author: Ilya Stroy.
     3  // Contacts: iyuryevich@pm.me, https://github.com/qioalice
     4  // License: https://opensource.org/licenses/MIT
     5  
     6  package ekaerr
     7  
     8  type (
     9  	// Namespace is a special type that represents Error's and Class's abstract namespace
    10  	// and provides a mechanism of error and its classes classifying.
    11  	//
    12  	// Using Namespace's object you can register (and instantiate) a new Class.
    13  	// WARNING! If you create a two classes with the same name it will be a two
    14  	// different classes.
    15  	//
    16  	// DO NOT INSTANTIATE Namespace OBJECTS MANUALLY! THEY WILL NOT BE INITIALIZED
    17  	// PROPERLY AND WILL BE CONSIDERED BROKEN. THAT NAMESPACE IS NOT VALID.
    18  	// YOU CAN NOT CREATE A CLASS OBJECTS USING INVALID NAMESPACE.
    19  	//
    20  	// A Namespace is the entry point of the Error's creating chain:
    21  	// Namespace -> Class -> Error.
    22  	// Ekaerr provides you builtin namespace "CommonNamespace" you may use.
    23  	// But if you need your own namespace you can create it using NewNamespace().
    24  	//
    25  	// Namespace is a very lightweight datatype and all Namespace's methods
    26  	// (and functions that takes Namespace as argument) uses Namespace's object by value.
    27  	// It means there is no reason to pass Namespace's object by reference in your code.
    28  	Namespace struct {
    29  
    30  		// id is an unique per Namespace its ID.
    31  		// If it's == _ERR_INVALID_NAMESPACE_ID, the Class is considered broken
    32  		// (has been instantiated manually instead of using Namespace constructors).
    33  		id NamespaceID
    34  
    35  		// name is this Namespace's name specified by user at the creation.
    36  		// It's just a namespace's name as is, nothing more.
    37  		name string
    38  	}
    39  )
    40  
    41  // NewClass is a Class's constructor. Specify the Class's name 'name' and that is!
    42  // A new Class will be created and its copy is returned.
    43  //
    44  // Warnings:
    45  // A two classes with the same names is the two DIFFERENT classes!
    46  //
    47  // Requirements:
    48  // n must be valid Namespace object. Otherwise 'invalidClass' is returned.
    49  func (n Namespace) NewClass(name string) Class {
    50  	if !isValidNamespaceID(n.id) {
    51  		return invalidClass
    52  	}
    53  	fullName := name
    54  	if customNamespaceDefined() {
    55  		fullName = fullClassName(name, n.name, n.id)
    56  	}
    57  	return newClass(_ERR_INVALID_CLASS_ID, n.id, name, fullName)
    58  }
    59  
    60  // NewNamespace is a Namespace's constructor. Specify the Namespace's name 'name'
    61  // and that is! A new Namespace will be created and its copy is returned.
    62  //
    63  // Warnings:
    64  // A two namespaces with the same names is the two DIFFERENT namespaces!
    65  func NewNamespace(name string) Namespace {
    66  	return newNamespace(name, true)
    67  }