What is the name of the code format with spaces before attributes to keep them starting from the same column? - vim

We can see a kind of code format like this Object:
{
name: "Jason",
age: 28
gender: "male"
}
Or in Ruby, we can define a factory like this:
FactoryBot.define do
factory :company do
email { FFaker::Internet.email }
phone { FFaker::PhoneNumber.short_phone_number }
status { :processing }
name { FFaker::Company.name }
identity_no { FFaker::Identification.ssn }
end
end
The space between key and value (or method and attribute) are dynamically changing according to the longest key name (or method name).
How do you name this kind of format? And how can I do this in Vim?

Related

Terraform: how to pass multiple integers to JSON

Say we have three three variables in JSON:
{
name: "Someone",
age: 30,
code: 25
}
and I want to read this as a template in terraform and replace the integer values.
For one variable, I could solve this by modifying the JSON into:
{
name: "Someone",
age: "${age}",
code: 25
}
and then on my template, do this:
template = replace(file("file.json"), "\"$${age}\"", "$${age}")
but in this case, I have another variable called code which I have to update as well, is there any way to accomplish this? Thank you!
EDIT 1:
This is the JSON:
{
name: "Someone",
age: "${age}",
code: "${code}"
}
This is the terraform:
data "template_file" "part_1" {
template = file("file.json")
vars = {
age = var.age
code= var.code
}
}
This works all fine, but I need the 'age' and 'count' on JSON as integer, not as strings. If I keep JSON like this:
{
name: "Someone",
age: ${age},
code: ${code}
}
It gives me an inappropriate JSON format. That's why I add the replace to template, so I can remove the double quotes and place there an integer, but I can do this only once!
Try this:
template = "${replace( "${replace(file("file.json"), "\"$${age}\"", "$${age}")}" ), "\"$${code}\"", "$${code}" }"
Here I'm replacing value again after return first replacement. It works for me.
If you have more number of variable then you have to use multiple replace.
Hope it will be helpful

Django Graphene writing mutations with multiple layers of nested foreign keys

How do you write the schema and query for nested foreign keys? I checked the docs and found no examples of how to do this. So here was my attempt based on github and stackoverflow answers lets say I have these models:
class Address(models.Model):
name = models.CharField()
class Person(models.Model):
name = models.CharField()
address = models.ForeignKey('Address', on_delete=models.CASCADE, blank=False, null=False)
class Blog(models.Model):
person = models.ForeignKey('Person', on_delete=models.CASCADE, blank=False, null=False)
text = models.TextField()
I tried writing a schema like this:
class AddressInput(graphene.InputObjectType):
name = graphene.String(required=True)
class PersonInput(graphene.InputObjectType):
name = graphene.String(required=True)
address =graphene.Field(AddressInput)
class CreateNewBlog(graphene.Mutation):
blog=graphene.Field(BlogType)
class Arguments:
address_data = AddressInput()
person_data = PersonInput()
text = graphene.String()
#staticmethod
def mutate(root, info, person_data=None, address_data=None, **input):
address = Address.objects.create(name=address_data.name)
person = Person.objects.create(address=address, name=person_data.name)
blog = Blog.objects.create(person =person, text=input['text'])
blog.save()
return CreateNewBlog(blog=blog)
and I used a query like this:
mutation {
CreateNewBlog(person: { address: {name: "aaa"},
name: "First Last" }, text: "hi hi") {
Blog {
person{
name
address{
name
}
},
text
}
}
}
I got this error message:
{
"errors": [
{
"message": "'NoneType' object has no attribute 'name'",
"locations": [
{
"line": 32,
"column": 9
}
],
"path": [
"CreateNewBlog"
]
}
],
"data": {
"CreateNewBlog": null
}
}
I think the issue is in the way I wrote the schema.py file. Where it does not work to nest InputFields inside another InputField. Is there any other ways to write a single mutation?
Okay, a few things here. Firstly, you should generate your schema.graphql file, because that'll show you the actual final shape of the schema being built by Graphene, which would've made your debugging easier. Or you could use GraphiQL to test out your queries and lets its documentation and autocomplete do the heavy lifting for you.
But on to the specifics, your Graphene mutation definition is going to be generating a mutation that looks like this:
input AddressInput {
name: String!
}
input PersonInput {
name: String!
address: AddressInput
}
type CreateNewBlogOutput {
blog: Blog
}
type Mutation {
CreateNewBlog(addressData: AddressInput, personData: PersonInput, text: String): CreateNewBlogOutput!
}
Worth noting that there are two ways for you to supply an AddressInput here, one at root, and one inside PersonInput. This probably isn't what you're intending to do. Secondly, none of the root arguments are required, which is contributing to your error message being fairly unhelpful, because the problem is you're calling the mutation incorrect parameters but the query validator is letting it through because your types are very permissive.
I believe that if you were to run the mutation like the following, it'd actually work:
mutation {
CreateNewBlog(
personData: {
address: {
name: "aaa"
},
name: "First Last"
},
text: "hi hi"
) {
blog {
person {
name
address {
name
}
}
text
}
}
}
I only made two changes here, person was changed to personData (to match your mutation definition, Graphene does the conversation from snake case to camel case automatically), and Blog to blog in the field selection.
But lets go a little further, here's how I would have made the mutation.
class AddressInput(graphene.InputObjectType):
name = graphene.String(required=True)
class PersonInput(graphene.InputObjectType):
name = graphene.String(required=True)
address = AddressInput(required=True)
class CreateNewBlogInput(graphene.InputObjectType):
person = PersonInput(required=True)
text = graphene.String(required=True)
class CreateNewBlogPayload(graphene.ObjectType):
blog = graphene.Field(BlogType, required=True)
class CreateNewBlog(graphene.Mutation):
class Arguments:
input_data = CreateNewBlogInput(required=True, name="input")
Output = CreateNewBlogPayload
#staticmethod
def mutate(root, info, input_data):
address = Address.objects.create(name=input_data.person.address.name)
person = Person.objects.create(address=address, name=input_data.person.name)
blog = Blog.objects.create(person=person, text=input_data.text)
blog.save()
return CreateNewBlogPayload(blog=blog)
I'd also change CreateNewBlog to createNewBlog when constructing Graphene's mutation object, because the GraphQL convention is to use lower camel case for mutations.
Then you'd run it like this:
mutation {
createNewBlog(
input: {
person: {
address: {
name: "aaa"
},
name: "First Last"
}
text: "hi hi"
}
) {
blog {
person {
name
address {
name
}
}
text
}
}
}
Why wrap the entire input in a single input field? Mainly because it makes calling the mutation easier in the client when using variables, you can just provide single input arg of the correct shape rather than multiple.
// So instead of this
mutation OldCreateNewBlog($person: PersonInput, $text: String) {
createNewBlog(
personData: $person
text: $text
) {
blog {
person {
name
address {
name
}
}
text
}
}
}
// You have this
mutation NewCreateNewBlog($input: CreateNewBlogInput!) {
createNewBlog(
input: $input
) {
blog {
person {
name
address {
name
}
}
text
}
}
}
The latter makes it easier to change the input shape over time and only have to make the change in one place in client code.

How to filter Subscribers based on array of tags in Loopabck

I've two models - subscribers and tags
Sample data:
{
subscribers: [
{
name: "User 1",
tags: ["a","b"]
},
{
name: "User 2",
tags: ["c","d"]
}
]
}
I want to filter subscribers based on their tags.
If I give a and b tags, User 1 should list
If I give a and c tags,
both User 1 and User 2 should list
Here is what I tried:
Method 1:
tags is a column in subscribers model with array data type
/subscribers/?filter={"where":{"tags":{"inq":["a","b"]}}} // doesn't work
Method 2:
Created a separate table tags and set subscribers has many tags.
/subscribers/?filter={"where":{"tags":{"inq":["a","b"]}}} // doesn't work
How can I achieve this in Loopback without writing custom methods?
I've Postgresql as the connector
UPDATE
As mentioned in the loopback docs you should use inq not In
The inq operator checks whether the value of the specified property matches any of the values provided in an array. The general syntax is:
{where: { property: { inq: [val1, val2, ...]}}}
From this:
/subscribers/?filter={"where":{"tags":{"In":["a","b"]}}}
To this:
/subscribers/?filter={"where":{"tags":{"inq":["a","b"]}}}
Finally found a hack, using Regex! it's not a performant solution, but it works!!
{ "where": { "tags": { "regexp": "a|b" } } }

How to use Github GraphQL search to return user profiles with similar name( including login and display name)?

I have this simple search query
query test($name: String!) {
search(query: $name, type: USER, last: 100) {
edges {
textMatches {
fragment
property
highlights {
text
}
}
}
userCount
}
}
and say, for example, I would like to have the login information for all users from the search result. How would I do that? The results contain login or display names that matches the search text. Is there a way to find the login for those who only appear in the search because of their display name?
You were almost there! In "edges", you're dealing with an array of SearchResultItemEdge which contains a "node" property at the same level as "textMatches".
Since the node is a SearchResultItem, and can be one of User, Issue, PullRequest, etc, you have to specifically spread your node as a "User" in order to be able to access the login.
Give this query a try in the Explorer:
query test($name: String!) {
search(query: $name, type: USER, last: 100) {
edges {
node {
__typename
...on User {
login
}
}
textMatches {
fragment
property
highlights {
text
}
}
}
userCount
}
}

How do I limit the properties of a query based of a common subproperty?

Given the schema:
{
_id: ObjectID,
city:
{ units:
{ abc: {},
def: { tuid : String },
...
xxx: { tuid : String }
}
}
I would like to return, for a particular _id, all the properties of units who's subproperty tuid is, for example, 123.
I have searched for information about this but array operations keep popping up instead of what I need.
Thank you.

Resources