Полезный ответ beatcracker предлагает эффективное решение; позвольте мне дополнить его упрощенной версией, которая использует возможности PSv4 +:
# Sample input object
$object = [pscustomobject] @{
TableName = 'MyTable'
Description = 'Lorem ipsum dolor...'
AppArea = 'UserMgmt'
InitialVersionCode = $null
}
# Start with the list of candidate properties.
# For simplicity we target *all* properties of input object $obj
# but you could start with an explicit list as wellL
# $candidateProps = 'TableName', 'Description', 'AppArea', 'InitialVersionCode'
$candidateProps = $object.psobject.properties.Name
# Create the filtered list of those properties whose value is non-$null
# The .Where() method is a PSv4+ feature.
$nonNullProps = $candidateProps.Where({ $null -ne $object.$_ })
# Extract the list of non-null properties directly from the input object
# and convert to JSON.
$object | Select-Object $nonNullProps | ConvertTo-Json
Если вы используете JDK 9 или выше, CompletableFuture.orTimeout
позволит вам установить таймаут. Для более ранних версий вам необходимо реализовать это самостоятельно, возможно, путем планирования задачи, которая вызывает completeExceptionally
.
. Для ведения журнала вы можете использовать whenComplete
и проверять изменяемый аргумент, регистрируя его, если он присутствует. Примечание: в этом случае не проводится различие между таймаутами или другими отказами.