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

[Bug] Can't use private constructors #244

Open
Jei-sKappa opened this issue Nov 23, 2024 · 0 comments
Open

[Bug] Can't use private constructors #244

Jei-sKappa opened this issue Nov 23, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@Jei-sKappa
Copy link

This is my class:

import 'package:dart_mappable/dart_mappable.dart';

part 'my_class.mapper.dart';

@MappableClass()
class MyClass with MyClassMappable {
  const MyClass({
    required this.myString,
  }) : myNumber = null;

  @MappableConstructor()
  const MyClass._({
    required this.myString,
    required this.myNumber,
  });

  final String myString;
  final int? myNumber;
}

As you can see I want to use MyClass._ as constructor for dart mappable because I don't want to expose myNumber int the constructor.

The problem is that is the generated code:

// coverage:ignore-file
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint
// ignore_for_file: unused_element, unnecessary_cast, override_on_non_overriding_member
// ignore_for_file: strict_raw_type, inference_failure_on_untyped_parameter

part of 'my_class.dart';

class MyClassMapper extends ClassMapperBase<MyClass> {
  MyClassMapper._();

  static MyClassMapper? _instance;
  static MyClassMapper ensureInitialized() {
    if (_instance == null) {
      MapperContainer.globals.use(_instance = MyClassMapper._());
    }
    return _instance!;
  }

  @override
  final String id = 'MyClass';

  static String _$myString(MyClass v) => v.myString;
  static const Field<MyClass, String> _f$myString =
      Field('myString', _$myString, key: 'my_string');
  static int? _$myNumber(MyClass v) => v.myNumber;
  static const Field<MyClass, int> _f$myNumber =
      Field('myNumber', _$myNumber, key: 'my_number', mode: FieldMode.member);

  @override
  final MappableFields<MyClass> fields = const {
    #myString: _f$myString,
    #myNumber: _f$myNumber,
  };

  static MyClass _instantiate(DecodingData data) {
    return MyClass(myString: data.dec(_f$myString)); // This should use MyClass._
  }

  @override
  final Function instantiate = _instantiate;

  static MyClass fromMap(Map<String, dynamic> map) {
    return ensureInitialized().decodeMap<MyClass>(map);
  }

  static MyClass fromJson(String json) {
    return ensureInitialized().decodeJson<MyClass>(json);
  }
}

mixin MyClassMappable {
  String toJson() {
    return MyClassMapper.ensureInitialized()
        .encodeJson<MyClass>(this as MyClass);
  }

  Map<String, dynamic> toMap() {
    return MyClassMapper.ensureInitialized()
        .encodeMap<MyClass>(this as MyClass);
  }

  MyClassCopyWith<MyClass, MyClass, MyClass> get copyWith =>
      _MyClassCopyWithImpl(this as MyClass, $identity, $identity);
  @override
  String toString() {
    return MyClassMapper.ensureInitialized().stringifyValue(this as MyClass);
  }

  @override
  bool operator ==(Object other) {
    return MyClassMapper.ensureInitialized()
        .equalsValue(this as MyClass, other);
  }

  @override
  int get hashCode {
    return MyClassMapper.ensureInitialized().hashValue(this as MyClass);
  }
}

extension MyClassValueCopy<$R, $Out> on ObjectCopyWith<$R, MyClass, $Out> {
  MyClassCopyWith<$R, MyClass, $Out> get $asMyClass =>
      $base.as((v, t, t2) => _MyClassCopyWithImpl(v, t, t2));
}

abstract class MyClassCopyWith<$R, $In extends MyClass, $Out>
    implements ClassCopyWith<$R, $In, $Out> {
  $R call({String? myString});
  MyClassCopyWith<$R2, $In, $Out2> $chain<$R2, $Out2>(Then<$Out2, $R2> t);
}

class _MyClassCopyWithImpl<$R, $Out>
    extends ClassCopyWithBase<$R, MyClass, $Out>
    implements MyClassCopyWith<$R, MyClass, $Out> {
  _MyClassCopyWithImpl(super.value, super.then, super.then2);

  @override
  late final ClassMapperBase<MyClass> $mapper =
      MyClassMapper.ensureInitialized();
  @override
  $R call({String? myString}) =>
      $apply(FieldCopyWithData({if (myString != null) #myString: myString}));
  @override
  MyClass $make(CopyWithData data) =>
      MyClass(myString: data.get(#myString, or: $value.myString)); // This should use MyClass._

  @override
  MyClassCopyWith<$R2, MyClass, $Out2> $chain<$R2, $Out2>(Then<$Out2, $R2> t) =>
      _MyClassCopyWithImpl($value, $cast, t);
}

As you can see generated code is using MyClass constructor instead of MyClass._

@schultek schultek added the bug Something isn't working label Jan 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants