... | ... | @@ -167,6 +167,34 @@ var allKeysDHCPv4 []byte |
|
|
|
|
|
where `keaconfig_test_dhcp4_all_keys.json` is in the same directory and contains the string blob.
|
|
|
|
|
|
## Constructors
|
|
|
|
|
|
Golang has no special syntax or convention for a constructor. The constructors are not mandatory in Golang, and they are similar to the C++ factory functions rather than constructors. A constructor can have an arbitrary name, although it typically includes the `New` word by convention. The use of constructors is optional in Stork. However, the developers should take into consideration the pros and cons of using or not using the constructors for each case they are working on.
|
|
|
|
|
|
The arguments in favor of using constructors:
|
|
|
|
|
|
- Constructors can initialize unexported fields from another package.
|
|
|
- Constructors can validate the arguments and return an error when invalid values are specified.
|
|
|
|
|
|
The arguments in favor of initialising the structures directly:
|
|
|
|
|
|
- Golang does not support named function arguments, so it may be hard to infer the parameters and their order from the constructor call.
|
|
|
- Constructor has a hard-coded signature, adding a new parameter to the structure requires extending this signature and possibly modifying many constructor calls.
|
|
|
- A structure can have many valid data combinations, so multiple constructors may be needed. With many constructors, it is hard to come up with sufficiently descriptive, but not too long, constructor names.
|
|
|
- Each constructor has to be unit tested, and it increases the amount of code.
|
|
|
|
|
|
A simple constructor instantiating a `Subnet` structure and performing some basic validation should have the following signature:
|
|
|
|
|
|
```golang
|
|
|
func NewSubnet(id int64, prefix string) *Subnet
|
|
|
```
|
|
|
|
|
|
Alternatively, the function could return an error to report parameters' validation issues:
|
|
|
|
|
|
```golang
|
|
|
func NewSubnet(id int64, prefix string) (*Subnet, error)
|
|
|
```
|
|
|
|
|
|
# TypeScript Style
|
|
|
|
|
|
Used by UI. Details TBD
|
... | ... | |