get Custom Field Type - liferay

I'm trying to get the type of the custom fields I declared for the user...
int type = user.getExpandoBridge().getAttributeType(attribute);
The fact is that the function returns an Integer. There's any place where I can find the match between the identifiers(int type) and their type names?
Thank you,
Oriol

I believe you're looking for ExpandoColumnConstants.* (javadoc)

Related

ProtoBuf - Can a field support two possible datatypes?

is it possible for one field in protobuf to support/accept a string or an array of string.
This is my message:
message MessageA {
string fieldId =1;
string method = 2;
google.protobuf.Any value =3;
}
The reason for it being dynamic is because an array of string or a simple string can be the input. There are separate methods on what will happen if the payload is string[] or string.
Right now, I'm using any but I'm not sure what to do in the any file. I've read that oneof does not support array data types thats why I'm trying to make it work with any.
Here is the error message when I try to put a repeated inside a oneof:
No, basically. The main way of doing this would be to have two inner-types - one with a single-valued member, one with a repeated member, and have a oneof that covers the two inner-types. Or perhaps simpler: just have a repeated and separately have an enum, boolean, or other indicator to choose between the two possible meanings (so you can tell between "an array, that happened to have exactly one item" vs "the single value").
For anyone that might have the same problem as me, you can use google.protobuf.Value. There would be an additional object but it can be transformed using UsePipe so it shouldn't be a problem.
Here is the documentation: https://developers.google.com/protocol-buffers/docs/reference/google.protobuf#google.protobuf.Value

How to simple make string uppercase in F#?

Try to make as described:
To convert a string to lowercase, you can call the String.ToLower()
method
let makeUpperCase s =
s.ToUpper()
Get as result
error FS0072: Lookup on object of indeterminate type based on
information prior to this program point. A type annotation may be
needed prior to this program point to constrain the type of the
object. This may allow the lookup to be resolved.
If you want to invoke members of a value that is passed as an argument to a function, you have to give F# some hint about what the type of the value is. The best way to do this is using a type annotation:
let makeUpperCase (s:string) =
s.ToUpper()
F# compiler needs this, because it cannot figure out what ToUpper method are you trying to invoke as there may be many .NET objects that have a method of this name.

Bug on unconstrained Strings

Alloy appears to have a bug when relations include unconstrained Strings. No instance is found for the following:
sig foo{
bar: String,
yak: Int
}
pred show[]{one f:foo | f.yak=0}
run show for 1
If we change this to bar: Int, Alloy finds an instance with an arbitrary value.
This "known for ages" bug has thankfully a workaround.
For things to work, you need to "implicitly declare" some string values by using them in a fact or a predicate.
As an example, the following signature fact will allow bar to take its value in {"a","b","c"} :
sig foo{
bar: String,
yak: Int
}{
bar in "a"+"b"+"c"
}
You can also define a pool of string to be used instance wide as follows:
fact stringPool{
none!= "a"+"b"+"c"+"d"+"e"
}
See:
Provide Alloy with a "pool" of custom Strings
Problem in generation of world in predicate
How to use String in Alloy?
and so on ...
Thanks for the bug report. Can you file an issue?
BTW, strings are not well supported in Alloy. In general it's best to avoid any concrete types unless you really need them, and do everything with abstract ones. Most uses of integers aren't necessary either.

ObservableCollection: how to use the FirstOrDefault?

I have a custom class, with a property ID. We can name this class A, and the property ID.
I have an observable collection of A, and I would like to get the firstOrDefault to know if an object exists with a determined ID. Soy I do the following:
myObersableCollection.FirstOrDefault(a=>a.ID==2)
But I get the following error: it is not possible to convert implicitly A into bool.
What am I doing wrong?
Thanks.
Daimroc.
FirstOrDefault() returns a matching object, not a boolean.
If you just want to check whether there is a matching object, call .Any() instead.

Pass a dynamic variable in a static parameter of a method in C# 4

This is what I am trying to do:
public void method(int myVal, string myOtherVal)
{
// doing something
}
dynamic myVar = new SomeDynamicObjectImplementer();
method(myVar.IntProperty, myVar.StringProperty);
Note that my properties are also DynamicObjects. My problem is that the TryConvert method is never called and that I get a runtime error saying the method signature is invalid.
The following is working great:
string strVar = myVar.StringProperty;
int intVar = myVar.IntProperty;
And I would like to avoid
method((int)myVar.IntProperty, (string)myVar.StringProperty);
Is it possible to override something in DynamicObject to allow this? (or something else)
Thank you
The problem is your assumption that it will try a dynamic implicit convert on arguments of an dynamic invocation to make a method call work, this is not true.
When your arguments aren't statically typed, it will use the runtime type to find the best matching method (if the runtime type matches the static rules for implicit conversion to the argument type this will work too), since your your IntProperty,StringProperty seem to be returning a DynamicObject rather than an Int and a String or something that could statically be converter implicitly, this lookup will fail.
If SomeDynamicObjectImplementer could actually return an Int for IntProperty and a String for StringProperty your method call for without casting would actually work. It's also probably a better dynamic typing practice if you data type is based on the actually type of data rather than usage using try convert. You could add actually implicit convert methods for every possible type that you could return to that returned DynamicObject type, but that could cause strange resolution issues to depending on how much you are overloading.
However, another option to keep your dynamic implementation the same is to mix a little controlled static typing in, you can use ImpromputInterface (in nuget) to put an interface on top of a dynamic object, if you do that then the TryConvert method would be called on your returned DynamicObjects.
public interface ISomeStaticInterface{
int IntProperty {get;}
string StringProperty {get;}
}
...
var myVar = new SomeDynamicObjectImplementer().ActLike<ISomeStaticInterface>();
method(myVar.IntProperty, myVar.StringProperty);
Instead of using myVar.IntProperty can't you just put them in variables first, like you already did, and then use then for your method?
so method(intVar , strVar); seems fine. At least more elegant than casting.
Of course, if you're already certain your object will have IntProperty and StringProperty, why not just make an actual object with those properties instead?
Why are you doing the cast?
method(myVar.IntProperty, myVar.StringProperty);
should compile.
If the two properties must be the types suggested by the names then they shouldn't be dynamic.

Resources