
Security News
Federal Government Rescinds Software Supply Chain Mandates, Makes SBOMs Optional
The U.S. government is rolling back software supply chain mandates, shifting from mandatory SBOMs and attestations to a risk-based approach.
proxy.laosky.tk/micronull/optional
Advanced tools
The optional package in Go provides a generic type that can represent values which may or may not be set,
including the concept of null. This allows handling scenarios where a value might be missing or explicitly set to null in JSON.
The package is designed to be flexible and efficient, with support for custom marshalling and unmarshalling functions.
T.To install the optional package, use the following command:
go get github.com/micronull/optional
Here's a simple example demonstrating how to use the Type struct and its methods:
package main
import (
"encoding/json"
"fmt"
"github.com/micronull/optional"
)
func main() {
// Create a new optional value that is not null
optVal := optional.New[string]("hello", false)
fmt.Println(optVal.IsSet()) // Output: true
fmt.Println(optVal.IsSetNull()) // Output: false
// Marshal the optional value to JSON
jsonBytes, _ := json.Marshal(optVal)
fmt.Println(string(jsonBytes)) // Output: "hello"
// Create a new optional value that is explicitly null
optNull := optional.New[string]("", true)
fmt.Println(optNull.IsSet()) // Output: false
fmt.Println(optNull.IsSetNull()) // Output: true
// Marshal the null optional value to JSON
jsonBytes, _ = json.Marshal(optNull)
fmt.Println(string(jsonBytes)) // Output: null
// Unmarshal JSON into an optional value
var opt optional.Type[string]
fmt.Println(opt.IsSet()) // Output: false
fmt.Println(opt.IsSetNull()) // Output: false
json.Unmarshal([]byte(`"world"`), &opt)
fmt.Println(opt.V) // Output: world
fmt.Println(opt.IsSet()) // Output: true
fmt.Println(opt.IsSetNull()) // Output: false
json.Unmarshal([]byte(`null`), &opt)
fmt.Println(opt.IsSet()) // Output: true
fmt.Println(opt.IsSetNull()) // Output: true
}
You can replace the default JSON marshalling and unmarshalling functions with your own implementations, such as using json-iterator:
package main
import (
"fmt"
jsoniter "github.com/json-iterator/go"
"github.com/micronull/optional"
)
func init() {
// Replace the default marshaller with json-iterator's marshaller
optional.ChangeMarshal(jsoniter.Marshal)
// Replace the default unmarshaller with json-iterator's unmarshaller
optional.ChangeUnmarshal(jsoniter.Unmarshal)
}
func main() {
optVal := optional.New[string]("hello", false)
jsonBytes, _ := jsoniter.Marshal(optVal)
fmt.Println(string(jsonBytes)) // Output: "hello"
}
Contributions are welcome! If you have any suggestions or find a bug, please open an issue on the GitHub repository.
This package is licensed under the MIT License. See the LICENSE file for more information.
FAQs
Unknown package
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
The U.S. government is rolling back software supply chain mandates, shifting from mandatory SBOMs and attestations to a risk-based approach.

Security News
crates.io adds a Security tab backed by RustSec advisories and narrows trusted publishing paths to reduce common CI publishing risks.

Research
/Security News
A Chrome extension claiming to hide Amazon ads was found secretly hijacking affiliate links, replacing creators’ tags with its own without user consent.