decodeMaterialCompileRequest function

MaterialCompileRequest decodeMaterialCompileRequest(
  1. String text
)

The request text holds, as encodeMaterialCompileRequest wrote it. Throws a FormatException for text that is not one. A key a later build adds is left alone.

Implementation

MaterialCompileRequest decodeMaterialCompileRequest(String text) {
  switch (jsonDecode(text)) {
    case {
          'name': final String name,
          'from': final String from,
          'source': final String source,
          'stages': final List<Object?> stages,
        } &&
        final Map<String, Object?> json:
      return MaterialCompileRequest(
        name: name,
        from: from,
        source: source,
        atRunTime: json['atRunTime'] == true,
        stages: <MaterialStageSource>[
          for (final stage in stages)
            switch (stage) {
              {
                'name': final String name,
                'kind': final String kind,
                'glsl': final String glsl,
              } =>
                MaterialStageSource(
                  name: name,
                  kind:
                      ShaderStageKind.byName(kind) ??
                      (throw MaterialCompileRequestException(
                        'no stage kind "$kind"',
                      )),
                  glsl: glsl,
                ),
              _ => throw const MaterialCompileRequestException(
                'a stage is not one',
              ),
            },
        ],
        engineSources: switch (json['engineSources']) {
          final Map<String, Object?> sources => <String, String>{
            for (final MapEntry(:key, :value) in sources.entries)
              if (value is String) key: value,
          },
          _ => const <String, String>{},
        },
        options: switch (json['options']) {
          final Map<String, Object?> options => options,
          _ => const <String, Object?>{},
        },
      );
    default:
      throw const MaterialCompileRequestException(
        'not a material compile request',
      );
  }
}