Skip to content

Instantly share code, notes, and snippets.

Avatar

Mike Schinkel mikeschinkel

View GitHub Profile
@mikeschinkel
mikeschinkel / StringLiteralsAreAPITA.md
Last active Jun 22, 2022
Managing String Literals is a PITA, not just in GoLang but in (All?) Other Languages
View StringLiteralsAreAPITA.md

Managing String Literals is a PITA,
Not Just in GoLang but in (All?) Other Languages

When working in Go, or Bash, or PHP, or any other language for that matter I find one of the most tedious tasks that are also very error prone is management of literal strings.

AFAIK no programming language has addressed this head on, ever. If I am right about that, it would be great if Go was the first.

Literal String Management

Management of literal strings occurs especially with URLs and local paths, but also with many other values that end up as literals used in source code. And there is not one good way to handle them, only several different ways all with major cons, and so developers are all over the map in how they handle string literals.

One approach is for a developer to assign a string literals to a named constant, and then use that name constant wherever the value is needed, e.g.:

@mikeschinkel
mikeschinkel / _README.md
Last active Apr 9, 2022
Potential update to GoLang `encoding/csv`. Provides a ReadFunc() method that allows tweaking the logic for processing a CSV file.
View _README.md

Making encoding/csv.Reader extensible

The simple idea behind these potential modifications is to add a function csv.ReadFunc() that can be passed a csv.LogicFunc when called where the signature of the passed closure and related type and consts would be.

In this case there is really only one evalation needed, but there could easily be multiple in other use-cases.

@mikeschinkel
mikeschinkel / benchmark_slice_aggregation.go
Last active Feb 3, 2022
GoLang benchmark for aggregating of multiple slice elements into a single slice
View benchmark_slice_aggregation.go
package test
//
// Initial source for benchmark from https://stackoverflow.com/a/40678026/102699
// Added BenchmarkConcatAppendPreAllocate()
//
import "testing"
func BenchmarkConcatCopyPreAllocate(b *testing.B) {
for n := 0; n < b.N; n++ {
B = concatCopyPreAllocate(slices)
View GrantLogonAsAService.ps1
Function GrantLogonAsAService([string]$Username) {
Write-Host "Grant Logon-as-a-Service for $Username"
$SecurityId = $null
try {
$Principal = new-object System.Security.Principal.NTAccount $Username
$SecurityId = $Principal.Translate([System.Security.Principal.SecurityIdentifier]).Value.ToString()
} catch {
Write-Host "Attempt to access SecurityID failed."
$SecurityId = $null
@mikeschinkel
mikeschinkel / Type.php
Created Oct 5, 2021
A reimagining of Sebastian Bergmann's Type.php if PHP had a "function list" declaration.
View Type.php
<?php declare(strict_types=1);
/*
* This file is part of sebastian/type.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\Type;
@mikeschinkel
mikeschinkel / _readme.md
Last active Apr 6, 2022
Potential approach for streamlined error handling in Go
View _readme.md

Proposal for Streamlined Error Handling in Go

This is a proposal for a way to simplify the error handling in Go.

The idea is to use attempt{} and recover{} blocks to separate the happy path from the error handling. NOTE: this is NOT exception handling as you would see in Java or PHP. More on why below.

In this proposal Go would allow the developer to omit capturing the last returned variable from a function call if and only if that call was within a attempt{} block. The Go compiler would handle providing access to the error instance via the geterror() function within the recover{} block if any function returns a non nil error as its last omitted parameter, i.e.:

attempt{
 data:= GetData()
@mikeschinkel
mikeschinkel / example.php
Last active Jul 27, 2021
How we might be able to move to using parameterized queries in WordPress core
View example.php
<?php
// Put this in wp-config.php
define('PREPARED_SQL_REQUIRED',true);
// A site builder who wants to use prepared statements and parameterized queries
// could run these in various hooks before $wpdb->query() below is run.
$question_fragment = $wpdb->prepare( '`question_id` = %d', $question_id );
$answer_fragment = $wpdb->prepare( '`answer_name` = %s', $new_answer );
$wpdb->compose('UPDATE %s polls SET vote = vote+1 WHERE %s AND %s',
@mikeschinkel
mikeschinkel / make_literal.php
Created Jun 26, 2021
A make_literal() function to get around functions that use is_literal() over-zealously
View make_literal.php
<?php
$safe_var = 'all your base they belong to us';
file_put_contents('/tmp/exploit.txt',$safe_var );
// imagine lots of stuff going on here...
$safe_var = file_get_contents('/tmp/exploit.txt');
function make_literal(string $non_literal):string {
$literal = '';
@mikeschinkel
mikeschinkel / try-break.php
Last active Mar 30, 2021
Illustrating the try-break pattern in action for PHP 8.x — hopefully to see PHP > 8.0 allow replacing `do{...}while(false};` with a naked `try{}`
View try-break.php
<?php
/**
* This shows our try-break example in action.
*/
function main() {
do {
$result = example(0,"hello");
if ($result->is_error() ) {
$result->print_error();
@mikeschinkel
mikeschinkel / _main.php
Last active Mar 16, 2021
Strawman psuedo-code for set of potential autoloader related classes for PHP core for proposing to internals
View _main.php
<?php
use PHP\Autoload;
use PHP\ClassmapLoader;
use PHP\CallableLoader;
Autoload::set_loader(new ClassmapLoader([
"Foo" => "path/to/Foo.php",
"Bar" => "another/path/to/Bar.php",
"Baz" => "yet/another/path/to/Baz.php",