How do I get the datatype for an App setting?

I'm working on an app where it would be useful to know the "Type" of a particular setting. I've tried object.getClass() and instanceof but the first one seems to be blocked and the second one does not recognize "bool" as datatype.

I was just about to ask this when I found the answer. Thought it could not hurt to post the answer again.

app.updateSetting("myBooleanValue", [value:false, type:"bool"]) 
log.info("myBooleanValue is: ${myBooleanValue}")
settings.myBooleanValue.properties.each{ 
    log.info ("${it}")
}

produces output: (log reads bottom to top)

class=class java.lang.Boolean
myBooleanValue is: false

4 Likes

I expect (and this is me recalling uni lectures from about 20+ years ago...) the second one would fail if the setting is a raw data type, or whatever the correct term is. e.g. strings are an object with a class definition, whereas I'm pretty sure a bool is the raw data type, but Boolean is a class / object. Not sure if that helps at all.

Unfortunately this is still an open question. Using the above method on different datatypes represents by color, boolean, enum and text controls on a page.

String reports as: [class:class java.lang.String, bytes:[37, 116, 105, 109, 101, 37], empty:false]
enum reports as: [class:class java.lang.String, bytes:[56, 48], empty:false]
Boolean reports as: [class:class java.lang.Boolean]
Color reports as: [class:class java.lang.String, bytes:[35, 49, 54, 49, 55, 49, 55], empty:false]

Another dead end apparently.

Can you share the use case you have for this? It's possible to get the underlying object type (what you'reseeing in logs above), but I don't know of a way for the setting itself. (EDIT: Actually there is; see Bruce's post below.) There could be another approach to meet your goals if you share more that might give someone an idea.

1 Like

I’m writing an app where I need to restore a group of settings, like a profile. Each group of settings has multiple colors, booleans enums and text values in them.

To work around the issue I keep a set of maps, one would contain all the color settings and then I know to use type;color when I use app.updateSettings.

It works OK but keeping internal lists is kludgy vs making a function call.

Thanks.

Use this method:

String getSettingType(String settingName)

3 Likes

I was going to suggest using instanceOf and checking the underlying data type yourself somehow (e.g., with an if or switch) since there are only a finite number of data types a setting can be, but the above approach is probably easier and something I didn't know existed. :slight_smile:

I do see you have at least one device in your screenshot. I want to say that there is no way to transfer the value of a selected device from one setting to another (e.g., for your capability.battery input above), but thinking about webCoRE, it looks like does this from the parent to child apps/pistons (across apps at that!), so there is probably something for that, too—but I'm not able to say how, if this even is a need for you. If the list of types you provided is exhaustive, I suppose not. The others shouldn't be an issue.

Thank you. Now works as expected.

image

Groovy handels everything as real objects.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.