Jaguar-dart / jaguar_serializer

Format (JSON, XML, protobuf, mongodb, etc) and platform (server, client) agnostic serialization framework
172 stars 34 forks source link

jaguar_serializer is not compatible with jaguar_orm #176

Open erwintan96 opened 5 years ago

erwintan96 commented 5 years ago
name: firsttry
description: First Project

version: 1.0.0+1

environment:
  sdk: ">=2.1.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter

  flutter_bloc: ^0.11.1
  equatable: ^0.2.0
  dio: ^2.1.2
  shared_preferences: ^0.5.2
  cupertino_icons: ^0.1.2
  device_info: ^0.4.0+1
  shimmer: ^1.0.0
  flutter_statusbar_manager: ^1.0.2
  material_design_icons_flutter: ^3.2.3595
  jaguar_serializer: ^2.2.12
  jaguar_orm: ^2.2.6

dev_dependencies:
  flutter_test:
    sdk: flutter
  jaguar_orm_gen:
  build_runner:
  jaguar_serializer_cli:

flutter:
  uses-material-design: true
import 'dart:async';
import 'package:jaguar_orm/jaguar_orm.dart';
import 'package:jaguar_serializer/jaguar_serializer.dart';

part 'pojo_brand.jorm.dart';
part 'pojo_brand.jser.dart';

class PojoBrand{

  @PrimaryKey(auto: true, isNullable: false)
  int id;
  String name;
  int status;
  PojoBrand({this.id, this.name, this.status = 1});

  static const String tableName = 'brand';
  @override
  bool operator ==(other) {
    if(other is PojoBrand){
      PojoBrand o = other as PojoBrand;
      return this.id == o.id && this.name == o.name;
    }
    return false;
  }
}

@GenSerializer()
class PojoBrandSerializer extends Serializer<PojoBrand>
    with _$PojoBrandSerializer {}

@GenBean()
class PojoBrandBean extends Bean<PojoBrand> with _PojoBrandBean {
  PojoBrandBean(Adapter adapter) : super(adapter);

  String get tableName => 'brand';
}
// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'pojo_brand.dart';

// **************************************************************************
// BeanGenerator
// **************************************************************************

abstract class _PojoBrandBean implements Bean<PojoBrand> {
  final id = IntField('id');
  final name = StrField('name');
  final status = IntField('status');
  Map<String, Field> _fields;
  Map<String, Field> get fields => _fields ??= {
        id.name: id,
        name.name: name,
        status.name: status,
      };
  PojoBrand fromMap(Map map) {
    PojoBrand model = PojoBrand();
    model.id = adapter.parseValue(map['id']);
    model.name = adapter.parseValue(map['name']);
    model.status = adapter.parseValue(map['status']);

    return model;
  }

  List<SetColumn> toSetColumns(PojoBrand model,
      {bool update = false, Set<String> only, bool onlyNonNull = false}) {
    List<SetColumn> ret = [];

    if (only == null && !onlyNonNull) {
      if (model.id != null) {
        ret.add(id.set(model.id));
      }
      ret.add(name.set(model.name));
      ret.add(status.set(model.status));
    } else if (only != null) {
      if (model.id != null) {
        if (only.contains(id.name)) ret.add(id.set(model.id));
      }
      if (only.contains(name.name)) ret.add(name.set(model.name));
      if (only.contains(status.name)) ret.add(status.set(model.status));
    } else /* if (onlyNonNull) */ {
      if (model.id != null) {
        ret.add(id.set(model.id));
      }
      if (model.name != null) {
        ret.add(name.set(model.name));
      }
      if (model.status != null) {
        ret.add(status.set(model.status));
      }
    }

    return ret;
  }

  Future<void> createTable({bool ifNotExists = false}) async {
    final st = Sql.create(tableName, ifNotExists: ifNotExists);
    st.addInt(id.name, primary: true, autoIncrement: true, isNullable: false);
    st.addStr(name.name, isNullable: false);
    st.addInt(status.name, isNullable: false);
    return adapter.createTable(st);
  }

  Future<dynamic> insert(PojoBrand model,
      {bool cascade = false,
      bool onlyNonNull = false,
      Set<String> only}) async {
    final Insert insert = inserter
        .setMany(toSetColumns(model, only: only, onlyNonNull: onlyNonNull))
        .id(id.name);
    var retId = await adapter.insert(insert);
    if (cascade) {
      PojoBrand newModel;
    }
    return retId;
  }

  Future<void> insertMany(List<PojoBrand> models,
      {bool onlyNonNull = false, Set<String> only}) async {
    final List<List<SetColumn>> data = models
        .map((model) =>
            toSetColumns(model, only: only, onlyNonNull: onlyNonNull))
        .toList();
    final InsertMany insert = inserters.addAll(data);
    await adapter.insertMany(insert);
    return;
  }

  Future<dynamic> upsert(PojoBrand model,
      {bool cascade = false,
      Set<String> only,
      bool onlyNonNull = false}) async {
    final Upsert upsert = upserter
        .setMany(toSetColumns(model, only: only, onlyNonNull: onlyNonNull))
        .id(id.name);
    var retId = await adapter.upsert(upsert);
    if (cascade) {
      PojoBrand newModel;
    }
    return retId;
  }

  Future<void> upsertMany(List<PojoBrand> models,
      {bool onlyNonNull = false, Set<String> only}) async {
    final List<List<SetColumn>> data = [];
    for (var i = 0; i < models.length; ++i) {
      var model = models[i];
      data.add(
          toSetColumns(model, only: only, onlyNonNull: onlyNonNull).toList());
    }
    final UpsertMany upsert = upserters.addAll(data);
    await adapter.upsertMany(upsert);
    return;
  }

  Future<int> update(PojoBrand model,
      {bool cascade = false,
      bool associate = false,
      Set<String> only,
      bool onlyNonNull = false}) async {
    final Update update = updater
        .where(this.id.eq(model.id))
        .setMany(toSetColumns(model, only: only, onlyNonNull: onlyNonNull));
    return adapter.update(update);
  }

  Future<void> updateMany(List<PojoBrand> models,
      {bool onlyNonNull = false, Set<String> only}) async {
    final List<List<SetColumn>> data = [];
    final List<Expression> where = [];
    for (var i = 0; i < models.length; ++i) {
      var model = models[i];
      data.add(
          toSetColumns(model, only: only, onlyNonNull: onlyNonNull).toList());
      where.add(this.id.eq(model.id));
    }
    final UpdateMany update = updaters.addAll(data, where);
    await adapter.updateMany(update);
    return;
  }

  Future<PojoBrand> find(int id,
      {bool preload = false, bool cascade = false}) async {
    final Find find = finder.where(this.id.eq(id));
    return await findOne(find);
  }

  Future<int> remove(int id) async {
    final Remove remove = remover.where(this.id.eq(id));
    return adapter.remove(remove);
  }

  Future<int> removeMany(List<PojoBrand> models) async {
// Return if models is empty. If this is not done, all records will be removed!
    if (models == null || models.isEmpty) return 0;
    final Remove remove = remover;
    for (final model in models) {
      remove.or(this.id.eq(model.id));
    }
    return adapter.remove(remove);
  }
}
// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'pojo_brand.dart';

// **************************************************************************
// JaguarSerializerGenerator
// **************************************************************************

abstract class _$PojoBrandSerializer implements Serializer<PojoBrand> {
  @override
  Map<String, dynamic> toMap(PojoBrand model) {
    if (model == null) return null;
    Map<String, dynamic> ret = <String, dynamic>{};
    setMapValue(ret, 'id', model.id);
    setMapValue(ret, 'name', model.name);
    setMapValue(ret, 'status', model.status);
    return ret;
  }

  @override
  PojoBrand fromMap(Map map) {
    if (map == null) return null;
    final obj = new PojoBrand();
    obj.id = map['id'] as int;
    obj.name = map['name'] as String;
    obj.status = map['status'] as int;
    return obj;
  }

}

error: The name 'Field' is defined in the libraries 'package:jaguar_query/src/core/core.dart' and 'package:jaguar_serializer/src/annotations/annotations.dart'. (ambiguous_import at [firsttry] lib\model\pojo_brand.jorm.dart:13)

error: '_PojoBrandBean.fields' ('() → Map<String, dynamic>') isn't a valid override of 'Bean.fields' ('() → Map<String, Field>'). (invalid_override at [firsttry] lib\model\pojo_brand.dart:33)

tejainece commented 5 years ago

My suggestion is to have ORM beans and Serializers in separate libraries.

You can them import/export them.

I will fix this in future versions of the library.