Go template to print an array's length - arrays

I have no Go experience. I use a tool, Podman, which is written in Go, it documents its --format option as,
-f, --format string Format the output to a Go template or json (default "json")
I can see what I want with,
podman inspect localhost/myimage --format "{{.RootFS.Layers.length}}"
But, if I want to get the count of how many elements there are what do I do? I've tried different fields with .count and .length and .len, but I always get,
can't evaluate field FieldName in type []digest.Digest
I've also tried invoking this as {{len(RootFS.Layers)}}. I'm just kind of brute forcing it. When I do the above, I get,
ERRO[0000] Error printing inspect output: template: all inspect:1: unexpected "(" in operand
What is the right Go Template to get an array's element count?

The go templating language and syntax is different from the go language itself - so len(RootFS.Layers) as you have seen does not work.
From the template docs, you are using the correct function len but the template syntax does not require parenthesis. So use:
{{ len .RootFS.Layers }}
FYI: a quick intro to Go templates.

Related

Wrapping HTML tags using Yield with Ruby

Ruby beginner here.
I am trying to understand yield and how to wrap HTML tags around it and I have been having issues with this code.
def tag (tag_name, attributes = nil)
"<#{tag_name}#{attributes}>#{yield}</#{tag_name}>"
end
style_tag = tag("div", ["class=", "red"]) do
tag("h1") do
"Google it"
end
end
my output is :
"<div[\"class=\", \"red\"]><h1>Google it</h1></div>"
Thank you
The issue is not with yield wich seems to be working fine, but with the attributes parameter. Or rather inserting the parameter into the string.
At the moment you are doing an implicit Array.to_s which is where the brackets come from. If you are sure the attributes string is correct, you can do a simple ...#{attributes.join} ... to join all elements to a proper string (provided the HTML syntax is correct and so on).

Angular NgTagsInputs - RegEx - Allow all values expect Strings

I'm using ngTagsInput where I want to allow all tags expect a few strings.
In their documentation it appears I can specify allowed tags through a regex but I can seem to work it out - been trying http://regexr.com/ for last hour with no luck.
So for example the strings I dont want to be accepted are:
story and global.
How do i go about to say everything is allowed expect those 2?
So for example storyBreaking is allowed but story is not.
Thanks.
try something like that:
(?<![\w\d])abc(?![\w\d]) where abc is your string to match!

Use Array of Values of Object in a Foreach Loop

I cannot seem to find anything about using the values of one property of an object in a foreach loop (without having the entire object placed into the loop).
I first create a function called UFGet-Servers that uses Get-ADComputer and returns the names of the servers in a specific OU in my environment and places them in an array. That's great, except that when I use the array in a foreach loop, each object that it grabs has #[Name=serverName] in it, which I cannot use in any useful manner. The following pseudo-code is an abbreviated example:
foreach($Computer in $ComputerNames){do code... code is adding the server name into a UNC path such as "\\$Computer\C$\"}
The problem with the above is that you can't add the whole object to a path -- it ends up looking like "\#[Name=serverNameHere]\C$\" which totally bombs out. How do I get rid of the "#[property=" part, and simply use the value as the $Computer in the loop?
What really weirds me out is that I can't find a straightforward article on this anywhere... would have thought everyone and their mom would have wanted to do something like this.
So, your issue isn't with ForEach loops, it is with string formatting. There are two ways that I know of to take care of what you need. First is going to be string formatting, which allows you to use {0}m {1} and so on to inject values into a string, providing that you follow the string with -f and a list of said values. Such as:
ForEach($Computer in $ComputerNames){
"The Server Path is \\{0}\Share$" -f $Computer.Name
}
The second way is a sub-expression (I'm sure somebody will correct me if I used the wrong term there). This one involves enclosing the variable and desired property (or a function, or whatever) inside $(). This will evaluate whatever is inside the parenthesis before evaluating the string. See my example:
ForEach($Computer in $ComputerNames){
"The Server Path is \\$($Computer.name)\Share$"
}

How to highlight math operators like '*' '+' '-' etc. for c language?

I'm writing C code and have some nice highlighting scheme but there is one thing I'd like to highlight and I can't figure out how. It's maths symbols like *;+;-;/;= ... and brackets {} [] (). I want them in the same color. I searched everywhere and the only thing I found was how to highlight specific keywords (I already used it for "FILE" keyword, I don't know why they didn't highlight it by default)
Looking at https://wiki.gnome.org/Apps/Gedit/NewLanguage which seem to be the gedit doc for how to set up cunstom sets of syntax highlights. This points to
https://developer.gnome.org/gtksourceview/stable/lang-tutorial.html
which says "Those regular expressions are PCRE regular expressions in the form /regex/options" following the documentation you can match individual characters like
<context id="escape" style-ref="escaped-character">
<match>\\.</match>
</context>
which matches \.. The doc also has examples of complete regular expressions
<match>http:\/\/[^\s]*</match>
for matching a URL.
Putting this together you might want something like
<context id="maths" style-ref="my-math-characters">
<match>(\+|\-|\*|\/)</match>
</context>
(...) is grouping, | is alternation, \+ etc quotes a meta-character. I don't use gedit myself so this is untested. It probably best to start with a simple set of symbols to see if things work at all and then expand for your complete set of characters. Watch out for < > & which have special meaning in xml so you may need to use < etc.

Eclipse - How do I view java arrays / collections better in debugger

Viewing/Searching java arrays and collections in the Eclipse Java debugger is tedious and time-consuming.
I tried this promising plugin (in alpha as of Aug 2012)
http://www.cvast.tuwien.ac.at/projects/visualdebugging/ArrayExplorer
But it freezes Eclipse for simple arrays beyond a few hundred elements.
I do use Detail formatters, but that still needs clicking on each element to see the values.
Are there any better ways to view this array/collection data?
Use the 'Expressions' tab.
There you can type in any number of expressions and have them evaluated in the current scope.
ie: collection.size(), collection.getValueAt(i), ect...
Eclipse > Preferences > Java > Debug >Detail Formatter
This may be close to what you are looking for. It is another tedious work to setup but once done you can see the value of objects in Expressions window.
Here is link to start
override toString method of your class and you will be able to see what you want to see. i'm attaching example to show you exactly that.
Even though i could not find a way to see them in nice table/array, i found a halfway workaround.
The solution is to define a static method in a throwaway class that takes the array as input and returns a string of concatenated values that one wants to quickly glance at. it could include the array index and newlines to view results formatted nicely. It can be fine tuned to print out only certain array indices to reduce clutter.
This static method can then be used in the watch area.

Resources