firebase-ios-sdk icon indicating copy to clipboard operation
firebase-ios-sdk copied to clipboard

Does @RemoteConfigProperty not parse JSON value?

Open dpyy opened this issue 2 years ago • 6 comments

Description

I expect @RemoteConfigProperty to parses JSON value into [String:[String]] but it doesn't

Reproducing the issue

No response

Firebase SDK Version

10

Xcode Version

14.2

Installation Method

Swift Package Manager

Firebase Product(s)

Remote Config

Targeted Platforms

iOS

Relevant Log Output

No response

If using Swift Package Manager, the project's Package.resolved

Expand Package.resolved snippet

Replace this line with the contents of your Package.resolved.

If using CocoaPods, the project's Podfile.lock

Expand Podfile.lock snippet

Replace this line with the contents of your Podfile.lock!

dpyy avatar Mar 09 '23 01:03 dpyy

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

google-oss-bot avatar Mar 09 '23 01:03 google-oss-bot

Would you share the specific example of what is not working?

See usage examples in our tests at https://github.com/firebase/firebase-ios-sdk/blob/master/FirebaseRemoteConfigSwift/Tests/SwiftAPI/PropertyWrapperTests.swift

paulb777 avatar Mar 11 '23 19:03 paulb777

Hey @dpyy. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

google-oss-bot avatar Mar 17 '23 01:03 google-oss-bot

Would you share the specific example of what is not working?

See usage examples in our tests at https://github.com/firebase/firebase-ios-sdk/blob/master/FirebaseRemoteConfigSwift/Tests/SwiftAPI/PropertyWrapperTests.swift

Is there actual documentation on @RemoteConfigProperty? Because I can't see any. So I guess the question back to you is, how exactly does @RemoteConfigProperty work for a JSON server value? What type is passed back?

dpyy avatar Mar 17 '23 01:03 dpyy

Hi @dpyy, unfortunately @RemoteConfigProperty is missing documentation (I'll add docs before closing this issue). In the meantime, to answer your question, the @RemoteConfigProperty property wrapper works on Decodable types, rather than an NSDictionary or NSArray that you're likely used to from the return type of jsonValue on RemoteConfigValue. I've added a couple examples below to get you started while I add documentation.

Top-Level Object

In this example the top-level entity in JSON is an object. This would have been returned as an NSDictionary when using the RemoteConfigValue.jsonValue approach.

Example JSON
{
  "recipeName": "Scrambled Eggs",
  "ingredients": [
    "eggs",
    "salt",
    "pepper",
    "butter"
  ],
  "cookMinutes": 5
}

Decodable

struct Recipe: Decodable {
  var recipeName: String
  var ingredients: [String]
  var cookMinutes: Int
}

Apple has some excellent documentation on creating Decodable (and Encodable / Codable) types to represent JSON in their Encoding and Decoding Custom Types guide. The Flight School Guide to Swift Codable is free right now and goes into even greater detail too.

Property-Wrapper

@RemoteConfigProperty(
  key: "recipe",
  fallback: Recipe(recipeName: "placeholder-recipe", ingredients: [], cookMinutes: 0)
) var recipe: Recipe

The fallback value is only used if Remote Config doesn't have a default value for the "recipe" key and it has not yet obtained the remotely configured value.

Top-Level Array

In this example the top-level entity in JSON is an array. This would have been returned as an NSArray when using the RemoteConfigValue.jsonValue approach.

Example JSON
[
  {
    "recipeName": "Scrambled Eggs",
    "ingredients": [
      "eggs",
      "salt",
      "pepper",
      "butter"
    ],
    "cookMinutes": 5
  },
  {
    "recipeName": "French Toast",
    "ingredients": [
      "eggs",
      "milk",
      "cinnamon",
      "bread",
      "butter",
      "maple syrup"
    ],
    "cookMinutes": 15
  }
]

Property-Wrapper

@RemoteConfigProperty(key: "recipe_list", fallback: []) var recipeList: [Recipe]

In this case the fallback value is just an empty array. Note that the wrapped value recipeList must be annotated with the type [Recipe] because Swift arrays only conform to Decodable if the contained type is known to be Decodable. An alternative approach would be to provide a fallback array containing a Recipe, e.g.:

@RemoteConfigProperty(
  key: "recipe_list",
  fallback: [Recipe(recipeName: "placeholder-recipe", ingredients: [], cookMinutes: 0)]
) var recipeList

Additional clarification

I expect @RemoteConfigProperty to parses JSON value into [String:[String]] but it doesn't

I just wanted to clarify whether this was a typo. Do you have example JSON payload that you'd expect to be parsed into [String:[String]]? As far as I'm aware, ["foo":["bar", "baz"]] wouldn't be valid JSON syntax but I could definitely be mis-guessing the expected input.

andrewheard avatar Mar 28 '23 19:03 andrewheard

Yeah it conforms to decodable @RemoteConfigProperty(key: "Json", fallback: structure(name: "Niket")) var value I do have my default value where name is Yash, but still the property returns me the struct with name Niket which is basically a fallback when it fails to retrieve. Why, please anyone?

Yashdeep123 avatar Mar 21 '24 09:03 Yashdeep123