Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does @RemoteConfigProperty not parse JSON value? #10917

Open
dpyy opened this issue Mar 9, 2023 · 6 comments
Open

Does @RemoteConfigProperty not parse JSON value? #10917

dpyy opened this issue Mar 9, 2023 · 6 comments

Comments

@dpyy
Copy link

dpyy commented Mar 9, 2023

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!
@google-oss-bot
Copy link

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

@paulb777
Copy link
Member

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

@google-oss-bot
Copy link

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!

@dpyy
Copy link
Author

dpyy commented Mar 17, 2023

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?

@andrewheard
Copy link
Contributor

andrewheard commented Mar 28, 2023

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.

@paulb777 paulb777 added the Docs label Aug 8, 2023
@Yashdeep123
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants