How to declare nullable field in Nim? - nim-lang

The stock_symbol is optional, it exists only for some companies, what would be proper way to declare it in Nim?
Do I have to use ref or there's other way?
type
Company = object
name: string
stock_symbol: string
echo Company(name: "Microsoft", stock_symbol: "MSFT")
echo Company(name: "Kinetic", stock_symbol: nil)
And similar question for composite type, usually you need to know both stock exchange and the symbol
type
SymbolWithExchange = object
exchange: string
symbol: string
Company2 = object
name: string
stock_symbol: SymbolWithExchange
echo Company2(
name: "Microsoft",
stock_symbol: SymbolWithExchange("NYSE", "MSFT")
)
echo Company2(name: "Kinetic", stock_symbol: nil)

Seems like Option[T] should be used
import options
type
Company = object
name: string
stock_symbol: Option[string]
echo Company(name: "Microsoft", stock_symbol: some("MSFT"))
echo Company(name: "Kinetic", stock_symbol: none(string))
and
import options
type
SymbolWithExchange = object
exchange: string
symbol: string
Company2 = object
name: string
stock_symbol: Option[SymbolWithExchange]
echo Company2(
name: "Microsoft",
stock_symbol: some(SymbolWithExchange(exchange: "NYSE", symbol: "MSFT"))
)
echo Company2(name: "Kinetic", stock_symbol: none(SymbolWithExchange))

Related

Generate JSON object in Groovy

For some reason I am not able to create JSON object in Groovy using JSONBuilder
Here is what I have but it comes back {}:
import groovy.json.JsonBuilder
JsonBuilder builder = new JsonBuilder()
builder {
name "Name"
description "Description"
type "schedule type"
schedule {
recurrenceType "one time"
start "${startDateTime}"
end "${endDateTime}"
}
scope {
entities ["${applicationId}"]
matches [
{
tags [
{
key "key name"
context "some context"
}
]
}
]
}
}
Does anyone know a simple way to create JSON object with nested elements?
I tend to find JsonOutput to be simpler to use for data that is already constructed. Yours would look like this:
groovy.json.JsonOutput.toJson(
[name: "Name",
description: "Description",
type: "schedule type",
schedule: [
recurrenceType: "one time",
start: "${startDateTime}",
end: "${endDateTime}"
],
scope: [
entities: ["${applicationId}"],
matches: [
[
tags: [
[
key: "key name",
context: "some context"
]
]
]
]
]]
)
If you are creating a JSON from Groovy objects, then you can use; JsonOutput
And if you have several values to pass and create a JSON object, then you can use; JsonGenerator
Or you can use JsonBuilder or StreamingJsonBuilder
check the groovy documentation

Cannot convert (untyped string constant) to *string [duplicate]

This question already has answers here:
Reference to string literals in Go
(5 answers)
Closed 2 years ago.
persistentvolumeclaim := &apiv1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "mysql-pv-claim",
},
Spec: apiv1.PersistentVolumeClaimSpec{
StorageClassName: "manual",
},
}
StorageClassName parameter takes pointer to string, but compiler gives error when i'm passing string "manual" into it.
You cannot get the address of a string constant/literal, but if you have a string local variable (set to the value you want) you can then pass the address of that local:
Declare a string local first and assign the constant string literal to it, then pass the address of that local as the parameter argument with the & operator:
persistentvolumeclaim := &apiv1.PersistentVolumeClaim {
manualStr := "manual"
ObjectMeta: metav1.ObjectMeta {
Name: "mysql-pv-claim",
},
Spec: apiv1.PersistentVolumeClaimSpec {
StorageClassName: &manualStr,
},
}
Use utils.StringPtr:
persistentvolumeclaim := &apiv1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "mysql-pv-claim",
},
Spec: apiv1.PersistentVolumeClaimSpec{
StorageClassName: utils.StringPtr("manual"),
},
}

How do I pass in multiple values in the nullfieldlist using the netsuite connector within Mulesoft dataweave

within my transform process I need to null out 2 fields and I am trying to do this in the mulesoft 4 dataweave, but it is only accounting for the second item and not both.
%dw 2.0
output application/java
import * from dw::core::Strings
---
{
customFieldList: {
customField: [
{ScriptId: "custentity_wip_commission_payment",
value: { internalId: payload.commisionpayment as String default ""} as Object {class: "org.mule.module.netsuite.extension.api.ListOrRecordRef"}
} as Object {class: "org.mule.module.netsuite.extension.api.SelectCustomFieldRef"},
{ScriptId: "custentity_wip_accounting_status",
value: { internalId: payload.accountingstatus as String default ""} as Object {class: "org.mule.module.netsuite.extension.api.ListOrRecordRef"}
} as Object {class: "org.mule.module.netsuite.extension.api.SelectCustomFieldRef"},
{ScriptId: "custentity_wip_retain",
value: { internalId: payload.retain as String default ""} as Object {class: "org.mule.module.netsuite.extension.api.ListOrRecordRef"}
} as Object {class: "org.mule.module.netsuite.extension.api.SelectCustomFieldRef"},
{ScriptId: "custentity_wip_schedule",
value: { internalId: payload.wipschedule as String default ""} as Object {class: "org.mule.module.netsuite.extension.api.ListOrRecordRef"}
} as Object {class: "org.mule.module.netsuite.extension.api.SelectCustomFieldRef"},
{scriptId: "custentity_wip_salesrep",
value: payload.salesrep as String default ""} as Object {class: "org.mule.module.netsuite.extension.api.StringCustomFieldRef"},
{ScriptId: "custentity_wip_agentcountry",
value: { internalId: payload.agentcountry as String default ""} as Object {class: "org.mule.module.netsuite.extension.api.ListOrRecordRef"}
} as Object {class: "org.mule.module.netsuite.extension.api.SelectCustomFieldRef"}
]
} as Object {class: "org.mule.module.netsuite.extension.api.CustomFieldList"},
nullFieldList: {"name": "custentity_wip_accounting_status",
"name": "custentity_wip_commission_payment"
},
I found the solution by creating the entry in a list form.
nullFieldList: {"name": ["custentity_wip_accounting_status","custentity_wip_commission_payment"]
},

Processing a large dataset of nested data

I have a rather large set of data which is structured in a somewhat unique fashion. It looks something like this:
foo:
- name: "some name"
location: "some location"
type: "someType"
bar:
- name: "A bar element"
location: "location here"
type: "someOtherType"
attachments:
- type: "attachmentTypeA"
name: "Attachment name"
- type: "attachmentTypeB"
name: "Attachment name"
baz:
- name: "another name"
location: "another location"
type: "anotherType"
qux:
- name: "My name here"
location: "My location here"
type: "SomeOtherTypeHere"
xyzzy:
- name: "Another name here"
location: "Another location here"
type: "anotherTypeHere"
bar:
- name: "Some name here"
location: "Some location here"
type: "typeHere"
attachments:
- type: "attachmentTypeA"
name: "attachment name here"
- type: "attachmentTypeA"
name: "attachment name here"
- type: "attachmentTypeB"
name: "attachment name here"
- name: "Another name here"
location: "Another location here"
type: "anotherTypeHere"
attachments:
- type: "attachmentTypeA"
name: "attachment name here"
- type: "attachmentTypeC"
name: "attachment name here"
- type: "attachmentTypeD"
name: "attachment name here"
- name: "Another baz listing"
location: "Baz location"
type: "bazTypeHere"
So basically, you have "foo" at the top level (and there can be more than one foo, but always at the top level). In general, the structure is:
foo > baz > qux > xyzzy > bar
However, any of the sub elements can be at the root, or under foo, provided they are in order. So these are valid:
foo
qux
xyzzy
bar
attachments
bar
attachments
As is this:
foo
baz
qux
xyzzy
bar
attachments
bar
attachments
qux
xyzzy
bar
attachments
bar
attachments
xyzzy
bar
attachments
bar
attachments
And so on. It's whacky, I know. But that's the dataset I inherited. I looked at the examples, in particular the DeserializeObjectGraph and LoadingAYamlStream examples. The DeserializeObjectGraph approach gets kind of crazy when the data is laid out like this. I finally gave up on it as it just got too hairy. The stream approach seems like a better fit, I think, but I'm running into troubles.
I am loading up the YAML as follows:
string contents = System.IO.File.ReadAllText ( fileName );
var input = new StringReader (contents);
var yaml = new YamlStream ();
yaml.Load (input);
As you can see, nothing fancy there. I'm just trying to get a "tree" of objects that I can then iterate through. I tried using the AllNodes property from the root node, but I can't for the life of me figure out how to iterate through them recursively in some manner than makes sense. I will also confess that I am a C# n00btard that is still learning (old C guy here), so bear with me!
Can anyone suggest an approach, or possibly some code or even pseudocode that might be able to help me out?

Substitute variable in an array from another array

Using groovy, how can I substitute the value of an array in another array variable?
For eg.:
def Env = [
'Env1',
'Env2',
'Env3'
]
def Job = [
[
name: "Job1",
label: "<$Env>",
action: #!/usr/bin/bash
blah
blah
],
[
name: "Job2",
label: "<$Env>",
action: #!/usr/bin/bash
blah
blah
]
]
I want the label field in the second array Job to be populated by every item in Env
If the code isn't dynamic you can just do:
def Env = ['Env1','Env2', 'Env3']
def Job = [
[
name: "Job1",
label: "<${Env[0]}>",
action: '#!/usr/bin/bash'
],
[
name: "Job2",
label: "<${Env[1]}>",
action: '#!/usr/bin/bash'
]
]
If it's dynamic you can do:
Job.eachWithIndex{ obj, idx ->
obj.label = Env[idx]
}

Resources