Using contains in conftest rules - open-policy-agent

I have following XML (it is a dotnet project file:
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<None Remove="appsettings.json" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="LoadA1Test" />
</ItemGroup>
</Project>
And following rules:
package main
project_reference = input.Project.ItemGroup[i].ProjectReference
deny[msg] {
not project_reference[i]["-Include"] = "XYZ"
msg = sprintf("in %s works \n", [project_reference[i]])
}
deny[msg] {
not contains(project_reference[i]["-Include"],"XYZ")
msg = sprintf("in %s doesn't work \n", [project_reference[i]])
}
When I try to validate with conftest the first rule fails as expected, but the second one passes. I tried a few options, but I don't know what I'm doing wrong.

A few things looks a bit odd:
Iteration over the item groups needs to go inside of the rules.
Use != x for checking if string is not equal to x.
And I don't think you'd need to reuse the i iterator since you're looking up a map key in the project reference. Something like this might do:
package main
deny[msg] {
project_reference := input.Project.ItemGroup[_].ProjectReference
project_reference["-Include"] != "XYZ"
msg = sprintf("-Include (%v) != XYZ", [project_reference["-Include"]])
}
deny[msg] {
project_reference := input.Project.ItemGroup[_].ProjectReference
not contains(project_reference["-Include"], "XYZ")
msg = sprintf("-Include: (%v) does not contain XYZ", [project_reference["-Include"]])
}
Though I'm not sure about the logic, since you'll always have the first rule fail if the -Include value isn't exactly XYZ, so there wouldn't be much point in adding another one to deny also if -Include did not contain that value.

Related

GtkBuilder F# Object reference not set to an instance of an object

I have a problem with making the mainwindow on my simple app run, The error given is that - Object reference not set to an instance of an object.
this happens when the app is getting debugged and the error occurs at handler.window1.ShowAll()
I did find some code online which hints at adding some member code as in member this.Whatever() = window1 however i have no idea if this is relevent to my code, or where to put it.
i am happy for any help you can give me as i have been trying all day to get this working in many ways and simply cannot.
namespace potato
module Main =
open System
open Gtk
type Handler()=class
[<Object>]
[<DefaultValue(true)>]
val mutable window1 : Window
end
[<EntryPoint>]
let Main(args) =
Application.Init()
let builder = new Builder("GUI.ui")
let handler = new Handler()
builder.Autoconnect(handler)
handler.window1.ShowAll()
Application.Run()
0
Here is the glade.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.20.0 -->
<interface>
<requires lib="gtk+" version="3.18"/>
<object class="GtkWindow" id="window1">
<property name="width_request">1024</property>
<property name="height_request">576</property>
<property name="can_focus">False</property>
<child>
<placeholder/>
</child>
</object>
</interface>
Right, the problem was right in front of my eyes, and i ended up having to go through old test projects to see and realise what #scrwtp was hinting at, this is the old working code fixed for gtk3 gtkbuilder.
namespace potato
module Main =
open System
open Gtk
type Handler()=class
[<Builder.Object>]
[<DefaultValue(true)>]
val mutable window1 : Window
end
let OnDelete (args:DeleteEventArgs) =
Application.Quit()
args.RetVal <- true
[<EntryPoint>]
let Main (args) =
Application.Init()
let gxml = new Builder("GUI.xml")
let handler = new Handler()
do gxml.Autoconnect(handler)
handler.window1.DeleteEvent
|> Event.add OnDelete
handler.window1.ShowAll()
Application.Run()
0
The reason, i now understand is that i had specified a handler and passed nothing to it, because nothing was passed it IE:(handler.window1.DeleteEvent) it simply wouldnt show when i called showall, hope this helps someone else with a similar problem

Prefix items in a comma delimited list stored in a property

I have a property which has as its value a comma delimited list of strings representing numbers. For example,
test.property=one,two,three
It could be any list of numbers but the form will be the same.
I have a directory called, for arguments sake, 'reals'.
Within it are subdirectories with names such as 'number.one', 'number.two' and 'number.three' etc and some other subdirectories I wish to ignore.
I would like to get a list of those subdirectories which correspond to entries in test.property
Something like
<dirset id="something" includes="${test.property}" dir="reals"/>
The problem here is that the items in the list defined by test.property would each need to be prefixed with 'number.' for this to work. I don't know how to do that, and that forms the first part of my question.
Is there any way around this using just the property I have described and not providing the task with a test.property list already in the correct format?
You can use the ant-contrib task PropertyRegex task, with something like this
<propertyregex property="${comma.delimed.nums}"
input="package.ABC.name"
regexp="\b(\w+)\b"
replace="number.\1"
global="true"
casesensitive="false" />
To make your dirset include the corresponding subdirectories edit + overwrite existing test.property with script task and builtin javascript engine :
<project>
<property name="test.property" value="one,two,three"/>
<echo>1. $${test.property} => ${test.property}</echo>
<script language="javascript">
<![CDATA[
var items = project.getProperty('test.property').split(',');
var s = "";
for (i = 0; i < items.length; i++) {
s += '*' + items[i] + ',';
}
project.setProperty('test.property', s.substring(0, s.length - 1));
]]>
</script>
<echo>2. $${test.property} => ${test.property}</echo>
<dirset id="something" includes="${test.property}" dir="C:\some\path"/>
<echo>Dirset includes => ${toString:something}</echo>
</project>
output :
[echo] 1. ${test.property} => one,two,three
[echo] 2. ${test.property} => *one,*two,*three
[echo] Dirset => number.one;number.three;number.two
If you want to create a new property instead of overwriting existing test.property use :
project.setProperty('whatever', s.substring(0, s.length - 1));
or
project.setNewProperty('whatever', s.substring(0, s.length - 1));
and use the new created property in include attribute of your dirset.

Reading multiple resource from jar file

I have jar file containing two folders each with a bunch of files (and possibly some other files in the root):
myjar.jar
> sub1
> file1.txt
> file2.txt
> file3.txt
> ...
> sub2
> file1.txt
> file2.txt
> file3.txt
> ...
> otherfile.txt
I have another project that depends on myjar.jar that contains an application that needs to read all the resources/text file and for each resource convert them into a string. Therefore I would like to do
URL url = this.class.getClassLoader().getResource("sub1");
// Somehow iterate all sub urls and convert to string
Any good suggestion for doing this? One approach could be:
String scheme = url.getProtocol();
if (!"jar".equals(scheme)) {
// Hm who if we simply want to run from the workspace???
throw new IllegalArgumentException("Unsupported scheme: " + scheme);
}
JarURLConnection con = (JarURLConnection) url.openConnection();
JarFile archive = con.getJarFile();
Enumeration<JarEntry> entries = archive.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
if (entry.getName().startsWith(folder)) {
System.out.println(entry.getName());
}
}
but I would like to avoid this low-level code.

MVC3 edit for decimal fields and localization

My locale uses a comma ,, and not a dot . for decimal separator.
In MVC3, when I open an Edit view where the decimal values are shown with
#Html.EditorFor(model => model.MyDecimalVal)
the value is shown correctly.
When I enter value with comma, I get error "Value is not a number" and if I enter value with dot, I get no error, but actually no value is saved.
How to handle this situation?
This blog post recommends overriding the default jQuery validate number and range rules in order to enable client-side support of the comma decimal separator.
To fix these problems, we can take the default implementation from the
jquery.validate.js file for the range() and number() functions. We
then create another .js file (say jQueryFixes.js), in which we
override these default functions with ones that contain support for
the comma as a decimal separator. The contents of the file should be
something like this:
$.validator.methods.range = function (value, element, param) {
var globalizedValue = value.replace(",", ".");
return this.optional(element) || (globalizedValue >= param[0] && globalizedValue <= param[1]);
}
$.validator.methods.number = function (value, element) {
return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:[\s\.,]\d{3})+)(?:[\.,]\d+)?$/.test(value);
}
Getting around with this by tweaking your validation logic has been already explained so here is a different approach.
Put the below code inside your web.config file under the <system.web> node if you would like to set the culture to en. It will take care of decimal delimiter:
<globalization culture="en-US" uiCulture="en" />
I am not sure but for DateTime, you are still bound to your server's locale.

Problem with BizTalk multi-input map

I have a map that takes 2 input messages, like this:
<ns0:Root>
<InputMessagePart_0>
<root>
<Indicator>1</Indicator>
<NewValue>AAA</NewValue>
</root>
<InputMessagePart_0>
<InputMessagePart_1>
<root>
<Value>BBB</Value>
</root>
<InputMessagePart_1>
</ns0:Root>
(Lots of the nodes are not shown for clarity)
The ouput message looks like this:
<Root>
<Value>AAA</Value>
</Root>
(It's identical to InputMessagePart_1)
If the Indicator is 1, I want Value to be replaced with NewValue. If it's 0, I want Value to stay the same. I used a scripting functoid with code like this:
public string Get_Value(string indicator, string value, string newValue)
{
if(indicator == "1")
{
return newValue;
}
else
{
return value;
}
}
I'm running into problems due to the fact that Value might not actually occur in the original InputMessagePart_1 - if it doesn't, I want to create it. With the script above, even though Indicator is 1, I'm not getting a return string when Value doesn't exist.
Any suggestions?
Updated: I did some further testing by removing the if/then logic and just returned a hard-coded string from the functoid, and I get the same results... it seems that just having the empty input kills the entire functionality of the functoid...
You should want to use the Equal functoid and test whether the value is 1. You'll then feed the result to the input of two functoids:
First, to a Value Mapping functoid, which is connected to the <New Value> tag in the first part of the source schema.
Second, to a Logical Not functoid, which is then connected to another Value Mapping functoid connected to the <Value> tag in the second part of the source schema.
If the <Indicator>; tag does not contain the expected value 1 or is not present in the source message, the Logical Equal functoid will return False, and the second branch of the map will execute.
It doesn't matter whether the Value tag is present in the second part of the source schema. If it is not, either one of the Value Mapping functoids will create it in the destination.
If you definitely need to depend upon the <Indicator> tag, you might want to use the Logical Existence functoid, that returns whether any specified input node appears in the source message.
If all else fails using the mapper, you might try switching to XSLT - see here how to scrape the XSLT out of your existing BTM.
The map that you are after looks straightforward:
<?xml version="1.0" encoding="utf-16"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > .. etc ... whatever you've scraped out
<xsl:output ...
<xsl:template match=...>
<ns1:Root>
<ns1:Value>
<xsl:choose>
<xsl:when test="/ns0:Root/ns0:InputMessagePart_0/ns0:root/ns0:Indicator/text()='1'">
<xsl:value-of select="/ns0:Root/ns0:InputMessagePart_0/ns0:root/ns0:NewValue/text()" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="/ns0:Root/ns0:InputMessagePart_1/ns0:root/ns0:Value/text()" />
</xsl:otherwise>
</xsl:choose>
<ns1:Value>
</ns1:Root>

Resources