rnmapbox / maps

A Mapbox react native module for creating custom maps
MIT License
2.21k stars 835 forks source link

[Bug]: Expression for literal object failing #2947

Open Gnative opened 1 year ago

Gnative commented 1 year ago

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

mfazekas commented 1 year ago

@Gnative thanks much for looking into this. This does sounds an upstream issue I've submitted a report to Mapbox maps iOS:

https://github.com/mapbox/mapbox-maps-ios/issues/1989

Gnative commented 1 year ago

Thanks @mfazekas

somasekharkakarla commented 1 year ago

Hi @mfazekas , any update on this issue

mfazekas commented 1 year ago

@somasekharkakarla as noted above it's an upstream issue I've submitted a bug report. Once it's fixed it there it should be working with rn as well.