language icon indicating copy to clipboard operation
language copied to clipboard

Different augmented output from macro during development and program running

Open alphamikle opened this issue 7 months ago • 0 comments

Hello!

I am at a bit of a loss.

When developing a macro, if I view the "generated" code using IDE tools (Intellij IDEA / VS Code - it doesn't matter), I see the following code:

Static Code
augment library 'file:///Users/alfa/dev/code/1_my_projects/copy_with_macro/example/main.dart';

import 'package:copy_with_macro/src/domain/serializable_to_json/logic/interface/serializable_to_json_interface.dart' as prefix0;
import 'dart:core' as prefix1;
import 'file:///Users/alfa/dev/code/1_my_projects/copy_with_macro/example/main.dart' as prefix2;
import 'package:copy_with_macro/src/domain/serializable_to_json/logic/converter/names_converter.dart' as prefix3;

augment class Second implements prefix0.ToJsonAble {
  external prefix1.Map<prefix1.String, prefix1.dynamic> toJson();
  const Second.$fromJson({
    required prefix1.String this.stringField,
    required prefix1.int this.intField,
    required prefix1.double this.doubleField,
    required prefix1.bool this.boolField,
    required prefix2.Tonality? this.enumField,
  });
  external static Second fromJson(prefix1.Map<prefix1.String, prefix1.dynamic> json);
  augment static prefix2.Second fromJson(prefix1.Map<prefix1.String, prefix1.dynamic> json, ) {
    return Second.$fromJson(
      stringField: json[r'stringField'] as prefix1.String, // common
      intField: json[r'intField'] as prefix1.int, // common
      doubleField: json[r'doubleField'] as prefix1.double, // common
      boolField: json[r'boolField'] as prefix1.bool, // common
      enumField: prefix2.Tonality.values.byName(prefix3.toSomeCase(json[r'enumField'].toString(), prefix3.NamingStrategy.plain)), // enum
    );
  }
}

And this is the code I'll see if I enable "Dart Exception Breakpoints" in IDEA and run the dart script with the test class. The Breakpoint is triggered on line 22 (enumField: json[r'enumField'] as prefix1.Tonality?) because inside the json with the specified key is, expectedly, a string, not enum.

Running Code
augment library 'file:///Users/alfa/dev/code/1_my_projects/copy_with_macro/example/main.dart';

import 'dart:core' as prefix0;
import 'file:///Users/alfa/dev/code/1_my_projects/copy_with_macro/example/main.dart' as prefix1;

augment class Second {
  external prefix0.Map<prefix0.String, prefix0.dynamic> toJson();
  const Second.$fromJson({
    required prefix0.String this.stringField,
    required prefix0.int this.intField,
    required prefix0.double this.doubleField,
    required prefix0.bool this.boolField,
    required prefix1.Tonality? this.enumField,
  });
  external static Second fromJson(prefix0.Map<prefix0.String, prefix0.dynamic> json);
  augment static prefix1.Second fromJson(prefix0.Map<prefix0.String, prefix0.dynamic> json, ) {
    return Second.$fromJson(
      stringField: json[r'stringField'] as prefix0.String, // common
      intField: json[r'intField'] as prefix0.int, // common
      doubleField: json[r'doubleField'] as prefix0.double, // common
      boolField: json[r'boolField'] as prefix0.bool, // common
      enumField: json[r'enumField'] as prefix1.Tonality?, // common
    );
  }
}

In addition, it can be seen that the "running script" does not implement the prefix0.ToJsonAble interface. The logic for generating the code responsible for converting strings to enum is based on checking the type of the constructor argument, whether it is an EnumDeclaration or not.

Globally the question is - is this normal at the current moment and will be fixed in the future, or is it something "weird" when the actual generated code is not fully created?

Flutter doctor:

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel master, 3.23.0-14.0.pre.75, on macOS 14.5 23F79 darwin-arm64, locale en-US)
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 15.4)
[✓] Chrome - develop for the web
[!] Android Studio (not installed)
[✓] IntelliJ IDEA Ultimate Edition (version 2024.1)
[✓] VS Code (version 1.90.2)
[✓] Connected device (3 available)
***
[✓] Network resources

! Doctor found issues in 1 category.

The exact same behavior was with an earlier version of Flutter (can't remember exactly, but I think it was 3.23.0-13.0.0.pre.274).

alphamikle avatar Jun 30 '24 08:06 alphamikle