Skip to content
This repository has been archived by the owner on Dec 20, 2023. It is now read-only.

Drop realmReadOnly user info key #57

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
* Improve error and warning messages when targetting an xcdatamodeld.
[Steven Watremez](https://github.com/StevenWatremez)
[#53](https://github.com/NijiDigital/gyro/pull/53)
* Stop supporting `realmReadOnly` user info key.
[Steven Watremez](https://github.com/StevenWatremez)
[#57](https://github.com/NijiDigital/gyro/pull/57)

## 1.4.0

Expand Down
53 changes: 2 additions & 51 deletions UserInfoKeys.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ Below are details about how to annotate your `.xcdatamodel` entities and attribu

- [Primary key](#primary-key)
- [Ignore attribute](#ignore-attribute)
- [Read only](#read-only)
- [Inverse Relationships](#inverse-relationships)
- [Optionnals fields and wrapper types](#optionnals-fields-and-wrapper-types)
- [Support Annotations](#support-annotations)
Expand Down Expand Up @@ -143,56 +142,6 @@ final class Shop: Object {
</details>


---

<a name="read-only"></a>
# Read only (DEPRECATED)

<details>
<summary> Information about read only 'user info'</summary>
On iOS/macOS, you can define attributes which are not persisted and whose value is computed dynamically.
To do so, add the following 'user info' to **the attribute**

| Key | Value |
|-----|-------|
| `realmReadOnly` | `the_code_source_to_generate` |


__Example__: On the `readOnly` attribute of the `Shop` entity:

![Read Only](documentation/read_only.png)
</details>

<details>
<summary>📑 Sample of the generated code in Objective-C (iOS)</summary>

```objc
// DO NOT EDIT | Generated by gyro

////////////////////////////////////////////////////////////////////////////////

#pragma mark - Imports

#import "RLMShop.h"

////////////////////////////////////////////////////////////////////////////////

#pragma mark - Implementation

@implementation RLMShop

#pragma mark - Superclass Overrides

- (NSString *)readOnly
{
return self.name;
}

@end
```
</details>


---

<a name="inverse-relationships"></a>
Expand Down Expand Up @@ -570,6 +519,8 @@ extension Shop: Mappable {

</details>

---

<a name="combine-jsonkeypath-and-enums"></a>
## Combine JSONKeyPath and enums

Expand Down
Binary file removed documentation/read_only.png
Binary file not shown.
9 changes: 2 additions & 7 deletions lib/gyro/parser/xcdatamodel/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module XCDataModel
#
class Attribute
attr_accessor :entity_name, :name, :type, :optional, :indexed, :default
attr_accessor :realm_ignored, :realm_read_only, :enum_type, :enum_values
attr_accessor :realm_ignored, :enum_type, :enum_values
attr_accessor :json_key_path, :json_values, :transformer, :comment, :support_annotation, :json_ignored

alias optional? optional
Expand All @@ -36,7 +36,6 @@ def initialize(attribute_xml, entity_name)
@default = attribute_xml.attributes['defaultValueString'].to_s
@type = attribute_xml.attributes['attributeType'].to_s.downcase.tr(' ', '_').to_sym
@realm_ignored = !Gyro::Parser::XCDataModel.user_info(attribute_xml, 'realmIgnored').empty?
@realm_read_only = Gyro::Parser::XCDataModel.user_info(attribute_xml, 'realmReadOnly')
@enum_type = Gyro::Parser::XCDataModel.user_info(attribute_xml, 'enumType')
@enum_values = Gyro::Parser::XCDataModel.user_info(attribute_xml, 'enumValues').split(',')
@json_key_path = Gyro::Parser::XCDataModel.user_info(attribute_xml, 'JSONKeyPath')
Expand All @@ -55,7 +54,7 @@ def to_h
'optional' => optional,
'indexed' => indexed,
'default' => default,
'realm_ignored' => realm_ignored, 'realm_read_only' => realm_read_only,
'realm_ignored' => realm_ignored,
'enum_type' => enum_type, 'enum_values' => enum_values,
'json_key_path' => json_key_path, 'json_values' => json_values,
'json_ignored' => json_ignored,
Expand All @@ -71,10 +70,6 @@ def enum?
!@enum_type.empty?
end

def read_only?
!@realm_read_only.empty?
end

def default?
[email protected]?
end
Expand Down
2 changes: 1 addition & 1 deletion lib/templates/android-java/inc/_attributes_enum.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public static final class Attributes {
{%- for attribute in entity.attributes -%}
{%- if attribute.realm_ignored == false or attribute.read_only == false -%}
{%- if attribute.realm_ignored == false -%}
{%- if attribute.comment.size > 0 %}
// {{ attribute.comment }}
{%- endif %}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{%- for attribute in entity.attributes %}

{%- if attribute.realm_read_only.size == 0 %}
{%- comment %} ******* CONVERT TYPE CAPTURE ******* {% endcomment -%}

{%- capture visibility %}
Expand All @@ -12,7 +10,7 @@
{%- endcapture %}

{%- capture convert_type %}
{%- if attribute.realm_read_only.size == 0 and attribute.enum_type.size > 0 -%}
{%- if attribute.enum_type.size > 0 -%}
String
{%- else -%}
{%- if params.use_wrappers.size > 0 and attribute.optional == true -%}
Expand Down Expand Up @@ -54,5 +52,4 @@
{{ visibility }} void set{{ name | titleize }}({%- if annotation.size > 0 %}{{ annotation | replace: "|"," " }} {% endif %}final {{ convert_type }} {{ name }}) {
this.{{ name }} = {{ name }};
}
{%- endif %}
{%- endfor %}
4 changes: 1 addition & 3 deletions lib/templates/android-java/inc/_attributes_properties.liquid
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{% for attribute in entity.attributes -%}
{%- if attribute.realm_read_only.size == 0 -%}
{%- comment %} ******* CONVERT TYPE CAPTURE ******* {% endcomment -%}
{%- capture convert_type -%}
{%- if attribute.realm_read_only.size == 0 and attribute.enum_type.size > 0 -%}
{%- if attribute.enum_type.size > 0 -%}
String
{%- else -%}
{%- if params.use_wrappers == "true" and attribute.optional == true -%}
Expand Down Expand Up @@ -49,5 +48,4 @@
@android.support.annotation.{{ attribute.support_annotation }}
{%- endif %}
private {{ convert_type }} {{ name }}{{ default_value }};
{%- endif -%}
{%- endfor -%}
2 changes: 1 addition & 1 deletion lib/templates/android-java/inc/_enum_getter_setter.liquid
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{%- for attribute in entity.attributes %}

{%- if attribute.realm_read_only.size == 0 and attribute.enum_type.size > 0 %}
{%- if attribute.enum_type.size > 0 %}
{% if params.support_annotations.size > 0 %}
@android.support.annotation.Nullable
{%- capture annotation -%}@android.support.annotation.NonNull {% endcapture -%}
Expand Down
2 changes: 1 addition & 1 deletion lib/templates/android-java/inc/_primitives.liquid
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{%- capture convert_type %}
{%- if attribute.realm_read_only.size == 0 and attribute.enum_type.size > 0 -%}
{%- if attribute.enum_type.size > 0 -%}
String
{%- else -%}
{%- if params.use_wrappers.size > 0 and attribute.optional == true -%}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{%- for relationship in entity.relationships %}
{%- if relationship.inverse == false %}
{%- capture relationship_type %}
{%- if relationship.realm_read_only.size == 0 and relationship.enum_type.size > 0 -%}
{%- if relationship.enum_type.size > 0 -%}
String
{%- else %}
{%- if relationship.destination.size == 0 %}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{%- for relationship in entity.relationships -%}
{%- capture relationship_type -%}
{%- if relationship.realm_read_only.size == 0 and relationship.enum_type.size > 0 -%}
{%- if relationship.enum_type.size > 0 -%}
String
{%- else -%}
{%- if relationship.destination.size == 0 -%}
Expand Down
2 changes: 1 addition & 1 deletion lib/templates/android-kotlin/inc/_attributes_enum.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

object Attributes {
{%- for attribute in entity.attributes -%}
{%- if attribute.realm_ignored == false or attribute.read_only == false -%}
{%- if attribute.realm_ignored == false -%}
{%- if attribute.comment.size > 0 %}
// {{ attribute.comment }}
{%- endif %}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
{% for attribute in entity.attributes -%}
{%- if attribute.realm_read_only.size == 0 -%}
{%- comment %} ******* CONVERT TYPE CAPTURE ******* {% endcomment -%}
{%- capture convert_type -%}
{%- if attribute.realm_read_only.size == 0 and attribute.enum_type.size > 0 -%}
{%- if attribute.enum_type.size > 0 -%}
String
{%- else -%}
{%- include 'inc/type_converter' -%}
{%- endif -%}
{%- endcapture -%}

{%- capture is_primitive %}
{%- if attribute.realm_read_only.size == 0 and attribute.enum_type.size > 0 -%}
{%- if attribute.enum_type.size > 0 -%}
false
{%- else -%}
{%- include 'inc/type_primitives' -%}
Expand Down Expand Up @@ -56,5 +55,4 @@
@SerializedName("{{ attribute.json_key_path }}")
{%- endif %}
var {{ name }}: {{ convert_type }}{{ nullable_type }}{{ default_value }}
{%- endif -%}
{%- endfor -%}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{%- for attribute in entity.attributes %}

{%- if attribute.realm_read_only.size == 0 and attribute.enum_type.size > 0 %}
{%- if attribute.enum_type.size > 0 %}
{%- assign name = attribute.name %}

fun get{{ name | titleize }}Enum(): {{ attribute.enum_type }}? = {{ attribute.enum_type }}.get({{ name }})
Expand Down
2 changes: 1 addition & 1 deletion lib/templates/android-kotlin/inc/_primitives.liquid
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{%- capture convert_type %}
{%- if attribute.realm_read_only.size == 0 and attribute.enum_type.size > 0 -%}
{%- if attribute.enum_type.size > 0 -%}
String
{%- else -%}
{%- if params.use_wrappers.size > 0 and attribute.optional == true -%}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{%- for relationship in entity.relationships -%}
{%- capture relationship_type -%}
{%- if relationship.realm_read_only.size == 0 and relationship.enum_type.size > 0 -%}
{%- if relationship.enum_type.size > 0 -%}
String
{%- else -%}
{%- if relationship.destination.size == 0 -%}
Expand Down
2 changes: 1 addition & 1 deletion lib/templates/swift3-variant/inc/_attributes_enum.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{% if entity.attributes.size > 0 %}
enum Attributes: String {
{%- for attribute in entity.attributes -%}
{%- if attribute.realm_ignored or attribute.realm_read_only.size == 0 -%}
{%- if attribute.realm_ignored == false -%}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 how come did it work before that == false?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how it worked before I remove realmReadOnly, it was a bug. But with unit tests I saw that attribute.realm_ignored is not a valid statement. So I copy existing comparison and now it works with attribute.realm_ignored == false

{%- if attribute.comment.size > 0 %}
/// {{ attribute.comment }}
{%- endif %}
Expand Down
20 changes: 9 additions & 11 deletions lib/templates/swift3-variant/inc/_attributes_properties.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,16 @@
{%- include 'inc/type_converter' -%}
{%- endcapture -%}

{%- if attribute.realm_read_only.size == 0 -%}
{%- if attribute.enum_values.size > 0 -%}
{%- comment -%} ******* GENERATE ENUM ATTRIBUTE PROPERTY ******* {% endcomment -%}
{%- include 'inc/enum_attribute_property' -%}
{%- else -%}
{%- if attribute.optional == true -%}
{% comment -%} ******* GENERATE OPTIONAL ATTRIBUTE PROPERTY ******* {% endcomment -%}
{%- include 'inc/optional_attribute_property' -%}
{%- else -%}
{%- comment -%} ******* GENERATE DEFAULT ATTRIBUTE PROPERTY ******* {% endcomment %}
{%- if attribute.enum_values.size > 0 -%}
{%- comment -%} ******* GENERATE ENUM ATTRIBUTE PROPERTY ******* {% endcomment -%}
{%- include 'inc/enum_attribute_property' -%}
{%- else -%}
{%- if attribute.optional == true -%}
{% comment -%} ******* GENERATE OPTIONAL ATTRIBUTE PROPERTY ******* {% endcomment -%}
{%- include 'inc/optional_attribute_property' -%}
{%- else -%}
{%- comment -%} ******* GENERATE DEFAULT ATTRIBUTE PROPERTY ******* {% endcomment %}
dynamic var {{ attribute.name }}: {{ convert_type }} = {{ default_value }}
{%- endif -%}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
Expand Down
2 changes: 1 addition & 1 deletion lib/templates/swift3/inc/_attributes_enum.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{% if entity.attributes.size > 0 %}
enum Attributes: String {
{%- for attribute in entity.attributes -%}
{%- if attribute.realm_ignored or attribute.realm_read_only.size == 0 -%}
{%- if attribute.realm_ignored == false -%}
{%- if attribute.comment.size > 0 %}
/// {{ attribute.comment }}
{%- endif %}
Expand Down
12 changes: 6 additions & 6 deletions lib/templates/swift3/inc/_attributes_properties.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
{%- include 'inc/type_converter' -%}
{%- endcapture -%}

{%- if attribute.realm_read_only.size == 0 -%}
{%- if attribute.enum_values.size > 0 -%}
{%- comment -%} ******* GENERATE ENUM ATTRIBUTE PROPERTY ******* {% endcomment -%}
{%- include 'inc/enum_attribute_property' -%}
{%- else -%}{%- if attribute.optional == true -%}
{%- if attribute.enum_values.size > 0 -%}
{%- comment -%} ******* GENERATE ENUM ATTRIBUTE PROPERTY ******* {% endcomment -%}
{%- include 'inc/enum_attribute_property' -%}
{%- else -%}
{%- if attribute.optional == true -%}
{% comment -%} ******* GENERATE OPTIONAL ATTRIBUTE PROPERTY ******* {% endcomment -%}
{%- include 'inc/optional_attribute_property' -%}
{%- else -%}
{%- comment -%} ******* GENERATE DEFAULT ATTRIBUTE PROPERTY ******* {% endcomment %}
@objc dynamic var {{ attribute.name }}: {{ convert_type }} = {{ default_value }} {%- if attribute.name == entity.identity_attribute %} /* Primary Key */{% endif -%}
{%- endif -%}{%- endif -%}
{%- endif -%}
{%- endif -%}
{%- endfor -%}

Expand Down
1 change: 0 additions & 1 deletion spec/fixtures/java/ignored/Shop.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public class Shop extends RealmObject {

public static final class Attributes {
public static final String NAME = "name";
public static final String READ_ONLY = "readOnly";

private Attributes() {
// Hide constructor
Expand Down
1 change: 0 additions & 1 deletion spec/fixtures/kotlin/ignored/Shop.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ open class Shop : RealmObject() {

object Attributes {
const val NAME: String = "name"
const val READ_ONLY: String = "readOnly"
}

object Relationships {
Expand Down
2 changes: 0 additions & 2 deletions spec/fixtures/swift/ignored/Shop.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import Foundation
final class Shop: Object {

enum Attributes: String {
case ignored = "ignored"
case ignored2 = "ignored2"
case name = "name"
}

Expand Down
9 changes: 2 additions & 7 deletions spec/fixtures/xcdatamodel/ignored.xcdatamodel/contents
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="12141" systemVersion="16E195" minimumToolsVersion="Xcode 4.3" sourceLanguage="Objective-C" userDefinedModelVersionIdentifier="">
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="13772" systemVersion="16G1212" minimumToolsVersion="Xcode 4.3" sourceLanguage="Objective-C" userDefinedModelVersionIdentifier="">
<entity name="Owner" syncable="YES">
<attribute name="name" optional="YES" attributeType="String" syncable="YES"/>
<relationship name="shop" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="Shop" inverseName="owner" inverseEntity="Shop" syncable="YES">
Expand All @@ -23,17 +23,12 @@
</userInfo>
</attribute>
<attribute name="name" attributeType="String" syncable="YES"/>
<attribute name="readOnly" attributeType="String" syncable="YES">
<userInfo>
<entry key="realmReadOnly" value="return self.name;"/>
</userInfo>
</attribute>
<relationship name="owner" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="Owner" inverseName="shop" inverseEntity="Owner" syncable="YES"/>
<relationship name="products" toMany="YES" deletionRule="Nullify" destinationEntity="Product" inverseName="shop" inverseEntity="Product" syncable="YES"/>
</entity>
<elements>
<element name="Owner" positionX="135" positionY="225" width="128" height="75"/>
<element name="Product" positionX="216" positionY="279" width="128" height="60"/>
<element name="Shop" positionX="54" positionY="144" width="128" height="135"/>
<element name="Shop" positionX="54" positionY="144" width="128" height="120"/>
</elements>
</model>