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

Cannot decode() non-map values #36

Open
bobjackman opened this issue Jan 21, 2021 · 0 comments
Open

Cannot decode() non-map values #36

bobjackman opened this issue Jan 21, 2021 · 0 comments

Comments

@bobjackman
Copy link

When using decode() to decode custom types, the class can't be instantiated.

Example:

import 'dart:io';

class Foo {
  static final Foo one = Foo._('one');
  static final Foo two = Foo._('two');
  static final Foo three = Foo._('three');
  
  final String name;
  
  Foo._(this.name);
}

class FooConfiguration implements Foo {
  Foo _delegate;
  
  FooConfiguration.fromFile(File file)                : super.fromFile(file);
  FooConfiguration.fromString(String yaml)            : super.fromString(yaml);
  FooConfiguration.fromMap(Map<dynamic, dynamic> yaml): super.fromMap(yaml);
  
  @override
  void decode(dynamic name) {
    name = name.toString().toLowerCase();
    
    if      (name == 'one'  ) _delegate = Foo.one;
    else if (name == 'two'  ) _delegate = Foo.two;
    else if (name == 'three') _delegate = Foo.three;
  }
  
  @override
  List<String> validate() {
    var errors = <String>[];
    if (this._delegate == null) {
      errors.add("Invalid value for `foo`. Must be one of one|two|three");
    }

    return errors;
  }
  
  String get name => _delegate.name;
}

void main() {
  // ======== Create File
  var fileContents = 'two';
  var filePath     = '/path/to/example.yaml';
  var file         = new File(filePath)..createSync()..writeAsStringSync(fileContents, flush: true);
  
  // ======== Load File
  var foo = new FooConfiguration.fromFile(filePath);
  // !!!!!!!!!!!!!!!!!!!!!!!!!
  // type 'String' is not a subtype of type 'Map<dynamic, dynamic>' in type cast
  // !!!!!!!!!!!!!!!!!!!!!!!!!
}

This error is coming from /lib/src/configuration.dart:19 which is both assuming and requiring that the contents of the file be a yaml object rather than any other valid yaml datatype.

There's a PR to fix this incoming...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant