Skip to content

Instantly share code, notes, and snippets.

View skalahonza's full-sized avatar

Jan Skála skalahonza

View GitHub Profile
@skalahonza
skalahonza / Ref.cs
Last active April 19, 2021 08:51
Async methods cannot use ref or out parameters. You can however use this Ref wrapper which is valid in terms of syntax
public class Ref<T>
{
public Ref() { }
public Ref(T value) { Value = value; }
public T Value { get; set; }
public override string ToString()
{
T value = Value;
return value == null ? "" : value.ToString();
}
@skalahonza
skalahonza / MongoDbExtensions.cs
Created February 16, 2021 08:29
MongoDb driver queryable interface integration with AutoMapper
public static class MongoDbExtensions
{
public static IMongoQueryable<TDestination> ProjectTo<TSource, TDestination>(this IQueryable<TSource> query, AutoMapper.IMapper autoMapper) =>
query.ProjectTo<BookListDTO>(autoMapper.ConfigurationProvider) as IMongoQueryable<TDestination>;
}
// usage ...
private readonly IMongoCollection<Book> _books;
private readonly IMapper _mapper;
// ...
@skalahonza
skalahonza / TableQueryExtensions.cs
Created February 5, 2021 08:13
Expose segmented query of Table Storage as IAsyncEnumerable
using Microsoft.Azure.Cosmos.Table;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
public static class TableQueryExtensions
{
public static async IAsyncEnumerable<T> ExecuteAsync<T>(this TableQuery<T> query, [EnumeratorCancellation] CancellationToken ct = default)
{
public class AzureTableStorageCache : IDistributedCache
{
private readonly AzureTableStorageCacheOptions _options;
private readonly CloudTable _table;
public AzureTableStorageCache(IOptions<AzureTableStorageCacheOptions> options)
{
_options = options.Value;
var tableClient = CloudStorageAccount.Parse(_options.ConnectionString).CreateCloudTableClient();
#!/bin/bash
while read line
do
echo "$line"
done < "${1:-/dev/stdin}"
@skalahonza
skalahonza / lister.sh
Last active December 26, 2020 09:49
Recursive LS. Created as a part of a cybersecurity challenge.
#!/bin/bash
recurse ()
(
recurse2 ()
{
[ $_recurse_stop -eq 1 ] && return
cd "$1" || return
pwd ## do whatever you want in the pwd
for entry in $(ls);
using Microsoft.Extensions.Logging;
using System;
using System.Linq;
public static class LoggingExtensions
{
public static IDisposable BeginPropertyScope(this ILogger logger, params (string key, object value)[] properties) =>
logger.BeginScope(properties.ToDictionary(p => p.key, p => p.value));
}
@skalahonza
skalahonza / SapDateTime.cs
Created October 27, 2020 14:00
Logic Apps expression for converting SAP datetime format into YYYY-MM-DD
using System;
using System.Linq;
namespace PasswordAttack
{
static class Program
{
const string EXPRESSION = "replace(concat(take(items('For_each')?['DATAB'],4),'-',take(skip(items('For_each')?['DATAB'],4),2),'-',take(skip(items('For_each')?['DATAB'],6),2)),'0000-00-00','1753-01-01')";
@skalahonza
skalahonza / OdataDateTimeQuery.cs
Created June 16, 2020 12:56
Construct OData date time query that compares date time part by part. Suitable for Logic Apps SQL server with Data Gateway
private static string GreaterOrEqual(string first, string second, string part) =>
$"{part}({first}) ge {part}({second})";
private static string GreaterThan(string first, string second, string part) =>
$"{part}({first}) gt {part}({second})";
private static string Equal(string first, string second, string part) =>
$"{part}({first}) eq {part}({second})";
/// <summary>
@skalahonza
skalahonza / add_ssh_cert.sh
Created May 13, 2020 13:37
Add public SSH certiphicate to remote host
cat ~/.ssh/id_rsa.pub | ssh user@hostname 'cat >> .ssh/authorized_keys'