Skip to content

Commit

Permalink
Merge pull request #78 from yancechen/main
Browse files Browse the repository at this point in the history
FlatBuffers 适配 null safe
  • Loading branch information
yancechen authored Apr 26, 2022
2 parents 2336059 + 93dc773 commit 85a4d37
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 71 deletions.
2 changes: 1 addition & 1 deletion README-zh.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
![social preview](social-dark.png)

<p align="center">
<a href="https://pub.dev/packages/fair"><img src="https://img.shields.io/badge/pub-2.3.0-orange" alt="pub"></a>
<a href="https://pub.dev/packages/fair"><img src="https://img.shields.io/badge/pub-2.4.0-orange" alt="pub"></a>
<a href="https://github.com/wuba/fair"><img src="https://img.shields.io/badge/platform-flutter-blue.svg" alt="github"></a>
<a href="https://fair.58.com/"><img src="https://img.shields.io/badge/doc-fair.58.com-green.svg" alt="doc"></a>
<a href="https://github.com/wuba/fair/LICENSE"><img src="https://img.shields.io/badge/license-BSD-green.svg" alt="license"></a>
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
![social preview](social-dark.png)

<p align="center">
<a href="https://pub.dev/packages/fair"><img src="https://img.shields.io/badge/pub-2.3.0-orange" alt="pub"></a>
<a href="https://pub.dev/packages/fair"><img src="https://img.shields.io/badge/pub-2.4.0-orange" alt="pub"></a>
<a href="https://github.com/wuba/fair"><img src="https://img.shields.io/badge/platform-flutter-blue.svg" alt="github"></a>
<a href="https://fair.58.com/"><img src="https://img.shields.io/badge/doc-fair.58.com-green.svg" alt="doc"></a>
<a href="https://github.com/wuba/fair/LICENSE"><img src="https://img.shields.io/badge/license-BSD-green.svg" alt="license"></a>
Expand Down Expand Up @@ -39,7 +39,7 @@ Assuming that the fair project and your own project are in the same folder:
```yaml
# add Fair dependency
dependencies:
fair: 2.3.0
fair: 2.4.0

# add compiler dependency
dev_dependencies:
Expand Down
3 changes: 3 additions & 0 deletions fair/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## [2.4.0]
* FlatBuffers supports generating bin files in a null safe environment.

## [2.3.0]
* Add null-safety
* Add support of flutter 2.5.0、2.5.1、2.5.2、2.5.3
Expand Down
4 changes: 2 additions & 2 deletions fair/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<img src="https://fair.58.com/logo.png">
</p>
<p align="center">
<a href="https://pub.dev/packages/fair"><img src="https://img.shields.io/badge/pub-2.3.0-orange" alt="pub"></a>
<a href="https://pub.dev/packages/fair"><img src="https://img.shields.io/badge/pub-2.4.0-orange" alt="pub"></a>
<a href="https://github.com/wuba/fair"><img src="https://img.shields.io/badge/platform-flutter-blue.svg" alt="github"></a>
<a href="https://fair.58.com/"><img src="https://img.shields.io/badge/doc-fair.58.com-green.svg" alt="doc"></a>
<a href="https://github.com/wuba/fair/LICENSE"><img src="https://img.shields.io/badge/license-BSD-green.svg" alt="license"></a>
Expand All @@ -19,7 +19,7 @@ We create Fair so we can dispatch UI changes to users as bundle(s), the way simi
Use Flutter Fair require few steps. Add dependency inside `pubspec.yaml`.
```yaml
dependencies:
fair: ^2.3.0
fair: ^2.4.0
```
Wrap your app with FairApp Widget.
Expand Down
Binary file modified fair/android/src/main/jniLibs/arm64-v8a/libfairflutter.so
Binary file not shown.
Binary file modified fair/android/src/main/jniLibs/armeabi-v7a/libfairflutter.so
Binary file not shown.
Binary file modified fair/android/src/main/jniLibs/x86/libfairflutter.so
Binary file not shown.
Binary file modified fair/android/src/main/jniLibs/x86_64/libfairflutter.so
Binary file not shown.
99 changes: 38 additions & 61 deletions fair/lib/src/internal/fair_bundle_generated.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
/*
* Copyright (C) 2005-present, 58.com. All rights reserved.
* Use of this source code is governed by a BSD type license that can be
* found in the LICENSE file.
*/

// automatically generated by the FlatBuffers compiler, do not modify
// ignore_for_file: unused_import, unused_field, unused_local_variable
// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable

import 'dart:typed_data' show Uint8List;
import 'package:flat_buffers/flat_buffers.dart' as fb;


class WidgetData {
WidgetData._(this._bc, this._bcOffset);

factory WidgetData(List<int> bytes) {
var rootRef = fb.BufferContext.fromBytes(bytes);
final rootRef = fb.BufferContext.fromBytes(bytes);
return reader.read(rootRef, 0);
}

Expand All @@ -23,27 +17,24 @@ class WidgetData {
final fb.BufferContext _bc;
final int _bcOffset;

String get className => const fb.StringReader().vTableGet(_bc, _bcOffset, 4, '');

List<int> get data => const fb.ListReader<int>(fb.Uint8Reader()).vTableGet(_bc, _bcOffset, 6, []);

List<int> get na => const fb.ListReader<int>(fb.Uint8Reader()).vTableGet(_bc, _bcOffset, 8, []);

List<int> get pa => const fb.ListReader<int>(fb.Uint8Reader()).vTableGet(_bc, _bcOffset, 10, []);

List<int> get methodMap => const fb.ListReader<int>(fb.Uint8Reader()).vTableGet(_bc, _bcOffset, 12, []);
String? get className => const fb.StringReader().vTableGetNullable(_bc, _bcOffset, 4);
List<int>? get data => const fb.Uint8ListReader().vTableGetNullable(_bc, _bcOffset, 6);
List<int>? get na => const fb.Uint8ListReader().vTableGetNullable(_bc, _bcOffset, 8);
List<int>? get pa => const fb.Uint8ListReader().vTableGetNullable(_bc, _bcOffset, 10);
List<int>? get methodMap => const fb.Uint8ListReader().vTableGetNullable(_bc, _bcOffset, 12);

@override
String toString() {
return 'WidgetData{className: $className, data: $data, na: $na, pa: $pa, methodMap: $methodMap}';
return 'WidgetData{className: ${className}, data: ${data}, na: ${na}, pa: ${pa}, methodMap: ${methodMap}}';
}
}

class _WidgetDataReader extends fb.TableReader<WidgetData> {
const _WidgetDataReader();

@override
WidgetData createObject(fb.BufferContext bc, int offset) => WidgetData._(bc, offset);
WidgetData createObject(fb.BufferContext bc, int offset) =>
WidgetData._(bc, offset);
}

class WidgetDataBuilder {
Expand All @@ -52,31 +43,26 @@ class WidgetDataBuilder {
final fb.Builder fbBuilder;

void begin() {
///todo 未知
fbBuilder.startTable(0);
fbBuilder.startTable(5);
}

int addClassNameOffset(int offset) {
int addClassNameOffset(int? offset) {
fbBuilder.addOffset(0, offset);
return fbBuilder.offset;
}

int addDataOffset(int offset) {
int addDataOffset(int? offset) {
fbBuilder.addOffset(1, offset);
return fbBuilder.offset;
}

int addNaOffset(int offset) {
int addNaOffset(int? offset) {
fbBuilder.addOffset(2, offset);
return fbBuilder.offset;
}

int addPaOffset(int offset) {
int addPaOffset(int? offset) {
fbBuilder.addOffset(3, offset);
return fbBuilder.offset;
}

int addMethodMapOffset(int offset) {
int addMethodMapOffset(int? offset) {
fbBuilder.addOffset(4, offset);
return fbBuilder.offset;
}
Expand All @@ -99,7 +85,8 @@ class WidgetDataObjectBuilder extends fb.ObjectBuilder {
List<int>? na,
List<int>? pa,
List<int>? methodMap,
}) : _className = className,
})
: _className = className,
_data = data,
_na = na,
_pa = pa,
Expand All @@ -108,40 +95,30 @@ class WidgetDataObjectBuilder extends fb.ObjectBuilder {
/// Finish building, and store into the [fbBuilder].
@override
int finish(fb.Builder fbBuilder) {
final classNameOffset = fbBuilder.writeString(_className ?? '');
final dataOffset = _data?.isNotEmpty == true ? fbBuilder.writeListUint8(_data ?? []) : null;
final naOffset = _na?.isNotEmpty == true ? fbBuilder.writeListUint8(_na ?? []) : null;
final paOffset = _pa?.isNotEmpty == true ? fbBuilder.writeListUint8(_pa ?? []) : null;
final methodMapOffset = _methodMap?.isNotEmpty == true ? fbBuilder.writeListUint8(_methodMap ?? []) : null;

///todo 未知
fbBuilder.startTable(0);
if (classNameOffset != null) {
fbBuilder.addOffset(0, classNameOffset);
}
if (dataOffset != null) {
fbBuilder.addOffset(1, dataOffset);
}
if (naOffset != null) {
fbBuilder.addOffset(2, naOffset);
}
if (paOffset != null) {
fbBuilder.addOffset(3, paOffset);
}
if (methodMapOffset != null) {
fbBuilder.addOffset(4, methodMapOffset);
}
final int? classNameOffset = _className == null ? null
: fbBuilder.writeString(_className!);
final int? dataOffset = _data == null ? null
: fbBuilder.writeListUint8(_data!);
final int? naOffset = _na == null ? null
: fbBuilder.writeListUint8(_na!);
final int? paOffset = _pa == null ? null
: fbBuilder.writeListUint8(_pa!);
final int? methodMapOffset = _methodMap == null ? null
: fbBuilder.writeListUint8(_methodMap!);
fbBuilder.startTable(5);
fbBuilder.addOffset(0, classNameOffset);
fbBuilder.addOffset(1, dataOffset);
fbBuilder.addOffset(2, naOffset);
fbBuilder.addOffset(3, paOffset);
fbBuilder.addOffset(4, methodMapOffset);
return fbBuilder.endTable();
}

/// Convenience method to serialize to byte list.
@override
Uint8List toBytes([String fileIdentifier = '']) {
var fbBuilder = fb.Builder();
var offset = finish(fbBuilder);

/// todo api修改可能有误
fbBuilder.finish(offset, fileIdentifier);
Uint8List toBytes([String? fileIdentifier]) {
final fbBuilder = fb.Builder(deduplicateTables: false);
fbBuilder.finish(finish(fbBuilder), fileIdentifier);
return fbBuilder.buffer;
}
}
8 changes: 4 additions & 4 deletions fair/lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ class FairUtils {

if (widgetData.data != null) {
map['data'] =
serializeReference(Reference.fromBuffer(_b(widgetData.data)));
serializeReference(Reference.fromBuffer(_b(widgetData.data!)));
}

if (widgetData.pa != null) {
map['pa'] = serializeReference(Reference.fromBuffer(_b(widgetData.pa)));
map['pa'] = serializeReference(Reference.fromBuffer(_b(widgetData.pa!)));
}
if (widgetData.na != null) {
map['na'] = serializeReference(Reference.fromBuffer(_b(widgetData.na)));
map['na'] = serializeReference(Reference.fromBuffer(_b(widgetData.na!)));
}

if (widgetData.methodMap != null) {
map['methodMap'] = serializeReference(Reference.fromBuffer(_b(widgetData.methodMap)));
map['methodMap'] = serializeReference(Reference.fromBuffer(_b(widgetData.methodMap!)));
}
var end2 = watch.elapsedMilliseconds;
log('[Fair] FlatBuffer construct: $end1 ms, serialize: $end2 ms');
Expand Down
2 changes: 1 addition & 1 deletion fair/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: fair
description: Flutter Fair is a package used to update widget dynamically.
version: 2.3.0
version: 2.4.0
homepage: https://fair.58.com/

environment:
Expand Down

0 comments on commit 85a4d37

Please sign in to comment.