Skip to content

Instantly share code, notes, and snippets.

@hlindberg

hlindberg/output.md

Last active Sep 2, 2018
Embed
What would you like to do?
Stringified output
expression type example output
literal undef nil
literal default "default"
literal integer 1
literal float 3.14
literal false false
literal true true
literal string "hello"
literal regexp "/this|that.*/"
string with single quote "ta'phoenix"
string with double quote "he said \"hi\""
user defined Object "Car({'regnbr' => 'abc123'})"
deferred "Deferred({'name' => 'func', 'arguments' => [1, 2, 3]})"
sensitive "#<Sensitive [value redacted]:70229233735920>"
Timestamp 'now' "2018-09-01T15:56:53.993630000 UTC"
array [1, 2, 3]
array with sensitive ["#<Sensitive [value redacted]:70229233766180>"]
hash with sensitive {"x"=>"#<Sensitive [value redacted]:70229233764800>"}
hash {"a"=>10, "b"=>20}
hash non Data key {"[\"funky\", \"key\"]"=>10}
hash Sensitive key {"#<Sensitive [value redacted]:70229233763220>"=>42}
hash __ptype key {"__ptype"=>10}
binary "aGVsbG8="
ascii-8bit string "AgMEaGVsbG8="
runtime 'alien' object "#Alien:0x007fbf0534a7f8"
runtime symbol "symbolic"
runtime 'alien' fact "#Alien:0x007fbf0534a7f8"
a type "Integer[0, 100]"
a user defined type "Car"
a self referencing type "Tree = Array[Variant[String, Tree]]"
require 'puppet_pal'
Puppet.initialize_settings
binary_string = "\x02\x03\x04hello".force_encoding('ascii-8bit')
class Alien
end
alien_fact = Alien.new
vars = {
'binary_val' => binary_string,
'alien_val' => []
}
facts = {
'alien_fact' => alien_fact
}
result = Puppet::Pal.in_tmp_environment('pal_env', modulepath: [], facts: facts, variables: vars) do |pal|
pal.with_catalog_compiler {|c|
vars['alien_val'][0] = alien_fact # punch a runtime value into the mutable array
vars['alien_val'][1] = :symbolic # punch a symbol value
source = <<-'SOURCE'
type Car = Object[attributes => {regnbr => String}];
type Tree = Array[Variant[String, Tree]]
$x = {
"literal undef" => undef,
"literal default" => default,
"literal integer" => 1,
"literal float" => 3.14,
"literal false" => false,
"literal true" => true,
"literal string" => "hello",
"literal regexp" => /this|that.*/,
"string with single quote" => "ta'phoenix",
"string with double quote" => "he said \"hi\"",
"user defined Object" => Car(abc123),
"deferred" => Deferred(func,[1,2,3]),
"sensitive" => Sensitive(hush),
"Timestamp 'now'" => Timestamp(),
"array" => [1,2,3],
"array with sensitive" => [Sensitive(hush)],
"hash with sensitive" => {x => Sensitive(hush)},
"hash" => { a=> 10, b=> 20},
"hash non Data key" => {[funky, key] => 10},
"hash Sensitive key" => {Sensitive(hush) => 42 },
"hash __ptype key" => {__ptype => 10},
"binary" => Binary("hello", "%s"),
"ascii-8bit string" => $binary_val,
"runtime 'alien' object" => $alien_val[0],
"runtime symbol" => $alien_val[1],
"runtime 'alien' fact" => $facts['alien_fact'],
"a type" => Integer[0,100],
"a user defined type" => Car,
"a self referencing type" => Tree,
}
$x
SOURCE
require 'byebug'; debugger
val = c.evaluate_string(source)
Puppet::Pops::Serialization::ToStringifiedConverter.convert(val,
:message_prefix => "The data given to sconv.rb"
)
}
end
puts "| expression type | example output |"
puts "| --- | --- |"
result.each_pair do |k,v|
puts "| #{k} | #{v.inspect} |"
end
@hlindberg

This comment has been minimized.

Copy link
Owner Author

@hlindberg hlindberg commented Aug 31, 2018

The "funky key" needs work - right now it turns the funky key into a string. It could use the alternative form
"Hash([["funky key"], 10])"

@hlindberg

This comment has been minimized.

Copy link
Owner Author

@hlindberg hlindberg commented Aug 31, 2018

"funky hash" could also be shown as Hash([["funky", "hash"], 10], ["b", 20]]) - i.e. with each k/v pair as a separate tuple. That may be easier read that just having alternating k/v values.

@hlindberg

This comment has been minimized.

Copy link
Owner Author

@hlindberg hlindberg commented Aug 31, 2018

ascii-8bit changed to output base64 (actually all non UTF-8)

@hlindberg

This comment has been minimized.

Copy link
Owner Author

@hlindberg hlindberg commented Sep 1, 2018

Gist is updated with more variants. Some notable changes:

  • Sensitive now has an object_id added because it could otherwise cause problems in hashes (since all instances looked the same)
  • facts are mangled (sanitized and this cold produce ascii-8bit strings; a subtle bug in puppet
  • an object.to_s may invoke object.inspect and a default implementation of that may result in ascii-8bit without content being binary (the problem in the bullet above)
    but this could also be the result of some alien object being stringified; code updated to deal with that
  • added examples of Sensitive in different places
  • added examples of data types
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment