rnmapbox / rnmapbox/maps

[Bug]: Expression for literal object failing

Open
#2,947 6 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug 🪲 iOS upstream v10
Dominant language
Kotlin
Stars
2.9k
Forks
947
Avg merge
6d 37m
Merged PRs (30d)
1

Description

Mapbox Implementation

Mapbox

Mapbox Version

default

Platform

iOS

@rnmapbox/maps version

#main

Standalone component to reproduce
import React from 'react';
import { MapView, CircleLayer, ShapeSource, Camera } from '@rnmapbox/maps';

const styles = {
  mapView: { flex: 1 },
  circleLayer: {
    circleRadiusTransition: { duration: 5000, delay: 0 },
    circleColor: '#ff0000',
  },
};

const features = {
  type: 'FeatureCollection',
  features: [
    {
      type: 'Feature',
      id: 'a-feature',
      properties: {
        icon: 'example',
        text: 'example-icon-and-label',
      },
      geometry: {
        type: 'Point',
        coordinates: [-74.00597, 40.71427],
      },
    },
  ],
};
class BugReportExample extends React.Component {
  state = {
    radius: 20,
  };

  render() {
    return (
      <MapView style={styles.mapView}>
        <Camera
          centerCoordinate={[-74.00597, 40.71427]}
          zoomLevel={14}
          animationDuration={0}
        />
        <ShapeSource id={'shape-source-id-0'} shape={features}>
          <CircleLayer
            id={'circle-layer'}
            style={{
              ...styles.circleLayer,
              ...{ circleRadius: this.state.radius },
              // Example of literal object that doesn't work
              circleOpacity: [
                'number',
                ['get', 'key1', ['literal', { key1: 1, key2: 0.5 }]],
                0.1,
              ],
              // Example of working literal array
              // circleOpacity: ['number', ['at', 0, ['literal', [1]]], 0.2],
            }}
          />
        </ShapeSource>
      </MapView>
    );
  }
}

export default BugReportExample;

Observed behavior and steps to reproduce

When using a literal Expression this should get the value from with in an object to use in the expression, this works on mapbox-gl but doesn't on this package.

Following the style expressions through from RN to RCTMGLStyleValue.swift line 191 and printing out the resulting decoded json. I can see the that the object is being decoded to a format rather then an object.

let data = try! JSONSerialization.data(withJSONObject: value, options: .prettyPrinted)

let decodedExpression = try JSONDecoder().decode(Expression.self, from: data)
          
print("Value:", value) // Print the value for debugging
print("Data:", String(data: data, encoding: .utf8) ?? "") // Print the data for debugging
print("Decoded Expression: \(decodedExpression)") // Print the decodedExpression for debugging

the output

Value: ["number", ["get", "key1", ["literal", ["key2": 0.5, "key1": 1]]], 0.2]
Data: [
  "number",
  [
    "get",
    "key1",
    [
      "literal",
      {
        "key2" : 0.5,
        "key1" : 1
      }
    ]
  ],
  0.20000000000000001
]
Decoded Expression: [number, [get, key1, [literal, format(MapboxMaps.FormatOptions(fontScaleValue: nil, textFontValue: nil, textColorValue: nil))]], 0.2]

There seems to be two different Expression definitions, Expression.swift and ExpressionOptions.swift, the later having this FormatOptions.. Is it possible that we are referencing the wrong Expression here ?

I'm not sure if this is happening because of how we originally input data to the decoder or if this issue is on Mapbox's side.

Expected behavior

literal should get value from object

Notes / preliminary analysis

No response

Additional links and references

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with RCTMGLStyleValue.swift around line 191, then compare the Expression.swift and ExpressionOptions.swift definitions mentioned in the report. Reproduce the literal-object expression with the supplied standalone component and inspect the decoded value. Done means the literal object remains an object so the expression can retrieve key1 as expected on iOS.

Written by the indexing model from the issue text.

Assessment

Tech stack
ios, javascript, react-native, swift
Domain
mobile
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.