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