With RestKit, I'm trying to create a request mapping that would convert a payment object into JSON. This object stores sub-payments, of which only one is valid. This valid payment is represented by the property selectedPayment. I think the typical approach for this is to use a relationship mapping however I don't want the selectedPayment to appear as a nested key. An example of the result of my current approach is below:
{
"requestUserId" : "6",
"payment" : {
"selectedPayment" : {
"amount" : "5",
"id" : 70,
"type" : "SOME_TYPE"
}
}
}
My desired output is the following:
{
"requestUserId" : "6",
"payment" : {
"amount" : "5",
"id" : 70,
"type" : "SOME_TYPE"
}
}
I have tried numerous options for my relationship mapping's destination key path: #root, #parent, "", nil. All of which have resulted in a crash (except "", which nests the selected payment under an empty key).
I could modify the attribute mappings of the request mapping that is used for selectedPayment to explicitly use #root or #parent but this would not be ideal as it would negatively affect other areas that mapping is used.
This seems like typical behaviour, converting a "deep" object representation into a "shallow" JSON representation, surely there is a straightforward way to do it?
Instead of using a relationship mapping, use key paths in your single mapping, navigate into the property and then extract the keys that you seeking.
Don't use a relationship mapping, instead use key paths in your single mapping to navigate into the property and extract the keys you want. So you would use selectedPayment.amount, selectedPayment.id and selectedPayment.type as the source key paths in the mapping and amount, id and type As the respective destination keys.
Related
I'm quite new to the world of data persistence and Core Data and I have a hard time representing a model for my app.
My app is a collection of forms and will feature a "template editor" that allows the user to choose what fields should these forms contain. For example, an user might want to create forms with the fields "name", "age" and "picture". Those three attributes can all be of different types (String, Int, and Data). Moreover, there's no guarantee what the user will select. They could add 45 different fields including custom ones (with their own titles) if they wanted.
My question is what would be an efficient way to model one of these forms in Core Data?
I thought about representing every attribute in the model, or making a dictionary attribute with all the fields… But since I'm a beginner, I'm afraid of making a wrong technical choice that could hurt the app's efficiency or ability to execute predicates, etc.
Thank you in advance 🙂
Since user can add n number of fields in a template, adding every fields into the DB is not good idea. This will lead to a big size and this will volute the db normalisation.
Better to convert the template into single json which can be stored in a file based on template name or id. If you have a DB already then you can link the file name into a user object. Same json can be used to upload to server or render the template again from the same json file.
Even you can position the fields based on id else even you can added position field in the json in order to maintain the field orders.
Example:
{
"template_id": "xyz",
"fields": {
"textfield": [
{
"label": "First Name",
"value": "Stack",
"id": 1
},
{
"label": "Last Name",
"value": "Overflow",
"id": 2
}
],
"image": [
{
"label": "Profile Photo",
"value": "/Document/Images/Profile.jpeg",
"id": 3
}
],
"date_picker": [
{
"label": "DOB",
"value": "01/01/1980",
"id": 4
}
]
}
}
Why not use this simple design for your models?. Here they are represented as structs but they are easily translated to entities
struct Form {
var name: String
var fields: [Field]
}
struct Field {
var name: String
var type: String //Maybe an enum in code
//var form: Form :in a struct you don't have this property but your entity will have it as a relationship
}
I believe dictionary model is best for this because you don't know what n how many fields user is going to use. But you can save those fields values in to string type and store it in ur core data.
Use Microsoft Excel to prototype and design a database. Once it's done, create your CoreData model.
When I use RestfulController in Grails to save data for an object, how can I prevent the client from applying changes to a related child object?
Given the following domain classes:
class Language {
String name
}
class TranslationText {
Language language
String text
}
And the following POST data for a TranslationText:
{
"language": { "id": 1, "name": "InvalidName" },
"text": "Some Text"
}
Here, I want to reference an existing Language resource (with ID=1), but I don't want the name to be altered by the client.
How can I save this resource with the text and language (based on ID), but discard the invalid language name property?
I want to modify RestfulController in the most minimal way possible, preserving default behavior as much as I can.
I think you need to configure the 'cascade' mapping property. This will tell GORM to evict the linked instance, so it won't be in the Hibernate session, changed to a new name and flushed to the DB :
class TranslationText {
Language language
String text
static mapping = {
language cascade: 'evict'
}
}
ref : http://docs.grails.org/3.1.x/ref/Database%20Mapping/cascade.html
Say I have the following API, where users can have zero or more registeredIds, which model identifiers by type (with effective dates).
Two examples of registeredIds include:
// Social Security Number
{
"id" : "111-11-1111",
"type" : "SSN",
"validFrom": 315554400000,
"validTo" : null,
"registrationAuthority": "United States Social Security Administration"
},
// Employee ID
{
"id" : "12345678",
"type" : "employee-id",
"validFrom": 1262325600000,
"validTo" : null,
"registrationAuthority": "YoYoDyne"
}
When Anonymous User requests an employee, e.g.,
https://api.usergrid.com/your-org/your-app/users/janedoe
Anonymous User should only get a single registeredId.type with the type value "employee-id." Administrators, however, should see both the "employee-id" and "SSN" registeredId.types.
How would Apache Usergrid apply access control by the registeredId.type? I know I can assign permissions, but this is too restrictive. Can I create some kind of Entity SubType? Or should I handle this through relationships?
Currently, Usergrid does not allow you to set property validation checks. One solution to this problem is to have separate "EmployeeID" entities, have a connection from each User to that their id entity and setup permissions so that only authenticated users can access the EmployeeID entities.
Given the following classes:
Simplified example:
class Query {
Institution institution
}
class Institution {
String name
}
With the following parameters being submitted: query.institution.id=20 and query.institution.name=Example
I would like to include only the institution id and ignore the name from being bound to the query's institution instance.
Is it possible with bindData to explicitly include associated instance's that are nested multiple levels like this?
I haven't seen any examples of this, aside from using the prefix for a single level of nesting, and the following does not seem to work:
Simplified example:
bindData(queryInstance, params, [include: [
'institution.id',
]], 'query')
The best practice is to filter your request parameters through a command object, which can then be used to generate any kind of query.
Command cmd = Command.getInstance()
bindData(cmd, request.params)
if (cmd.validate()) Query query = cmd.generateQuery()
This way you get the benefit of binding only to fields you expose on the command object, while validating and transforming incoming data without involving your domain.
I have the following scenario:
a web service that provides responses wrapped in the same object (described later)
the inner object can be completely different based on the endpoint being called
I am using GitHub's Mantle to do the model mapping in an iOS app
The base response wrapper is like this:
{
"UserId": "1234",
"Error": false,
"Message": "",
"DataObject": null
}
The DataObject property can be anything. i.e. it could be a DTOUser class like this:
{
"Username":"myusername",
"Email": "myemail#provider.com"
}
How would you map this with Mantle? I thought about creating a base response class that holds the wrapper data and then have all of my inner models inherit from that class. But then I'd have two classes for each response as the container would be a clone of the base class with the override of the transformer for the DataObject type, which is not elegant.
I thought about making a wrapper class and a separate inner model class and just map the response to the first and then map the same response to the second, but that's not elegant as well.
How would you do it?