tree: faff59899df920258939d4af225f3ce71afc7f3c [path history] [tgz]
  1. dart/
  2. fidl/
  3. go/
  4. hlcpp/
  5. llcpp/
  6. rust/
  7. BUILD.gn
  8. README.md
  9. test.json
  10. test_gn_sidecar.json
src/tests/fidl/source_compatibility/enum-member-add/README.md

Add an enum member

Overview

-initstep 1step 2step 3
fidllinklink
dartlinklink
golinklink
hlcpplinklink
llcpplinklink
rustlinklinklink

Initial State

FIDL

flexible enum Color {
    RED = 1;
    BLUE = 2;
};

Dart

fidllib.Color writer(String s) {
  if (s == 'red') {
    return fidllib.Color.red;
  } else if (s == 'blue') {
    return fidllib.Color.blue;
  } else {
    return fidllib.Color.$unknown;
  }
}

String reader(fidllib.Color color) {
  switch (color) {
    case fidllib.Color.blue:
      return 'blue';
    case fidllib.Color.red:
      return 'red';
    default:
      return '<unknown>';
  }
}

Go

func writer(s string) lib.Color {
	switch s {
	case "blue":
		return lib.ColorBlue
	case "red":
		return lib.ColorRed
	default:
		return lib.Color_Unknown
	}
}

func reader(color lib.Color) string {
	switch color {
	case lib.ColorBlue:
		return "blue"
	case lib.ColorRed:
		return "red"
	default:
		return "<unknown>"
	}
}

HLCPP

fidl_test::Color writer(std::string s) {
  if (s == "red") {
    return fidl_test::Color::RED;
  } else if (s == "blue") {
    return fidl_test::Color::BLUE;
  } else {
    return fidl_test::Color::Unknown();
  }
}

std::string reader(fidl_test::Color color) {
  switch (color) {
    case fidl_test::Color::RED:
      return "red";
    case fidl_test::Color::BLUE:
      return "blue";
    default:
      return "<unknown>";
  }
}

LLCPP

fidl_test::Color writer(std::string s) {
  if (s == "red") {
    return fidl_test::Color::RED;
  } else if (s == "blue") {
    return fidl_test::Color::BLUE;
  } else {
    return fidl_test::Color::Unknown();
  }
}

std::string reader(fidl_test::Color color) {
  switch (color) {
    case fidl_test::Color::RED:
      return "red";
    case fidl_test::Color::BLUE:
      return "blue";
    default:
      return "<unknown>";
  }
}

Rust

fn writer(s: &str) -> fidl_lib::Color {
    match s {
        "red" => fidl_lib::Color::Red,
        "blue" => fidl_lib::Color::Blue,
        _ => fidl_lib::Color::unknown(),
    }
}

fn reader(color: fidl_lib::Color) -> &'static str {
    match color {
        fidl_lib::Color::Red => "red",
        fidl_lib::Color::Blue => "blue",
        fidl_lib::ColorUnknown!() => "unknown",
    }
}

Update Source Code

Rust

  • Replace any unknown match arm macros with a catch-all case to handle the soon-to-be-added variant.
  fn writer(s: &str) -> fidl_lib::Color {
      match s {
          "red" => fidl_lib::Color::Red,
          "blue" => fidl_lib::Color::Blue,
          _ => fidl_lib::Color::unknown(),
      }
  }
  
  fn reader(color: fidl_lib::Color) -> &'static str {
      match color {
          fidl_lib::Color::Red => "red",
          fidl_lib::Color::Blue => "blue",
-         fidl_lib::ColorUnknown!() => "unknown",
+         _ => "unknown",
      }
  }

Update FIDL Library

  • Add the new member
  flexible enum Color {
      RED = 1;
      BLUE = 2;
+     YELLOW = 3;
  };

Update Source Code

Dart

  • Writers can now produce instances of the new enum member.
  fidllib.Color writer(String s) {
    if (s == 'red') {
      return fidllib.Color.red;
    } else if (s == 'blue') {
      return fidllib.Color.blue;
+   } else if (s == 'yellow') {
+     return fidllib.Color.yellow;
    } else {
      return fidllib.Color.$unknown;
    }
  }
  
  String reader(fidllib.Color color) {
    switch (color) {
      case fidllib.Color.blue:
        return 'blue';
      case fidllib.Color.red:
        return 'red';
+     case fidllib.Color.yellow:
+       return 'yellow';
      default:
        return '<unknown>';
    }
  }

Go

  • Writers can now produce instances of the new enum member.
  func writer(s string) lib.Color {
  	switch s {
  	case "blue":
  		return lib.ColorBlue
  	case "red":
  		return lib.ColorRed
+ 	case "yellow":
+ 		return lib.ColorYellow
  	default:
  		return lib.Color_Unknown
  	}
  }
  
  func reader(color lib.Color) string {
  	switch color {
  	case lib.ColorBlue:
  		return "blue"
  	case lib.ColorRed:
  		return "red"
+ 	case lib.ColorYellow:
+ 		return "yellow"
  	default:
  		return "<unknown>"
  	}
  }
  

HLCPP

  • Writers can now produce instances of the new enum member.
  fidl_test::Color writer(std::string s) {
    if (s == "red") {
      return fidl_test::Color::RED;
    } else if (s == "blue") {
      return fidl_test::Color::BLUE;
+   } else if (s == "yellow") {
+     return fidl_test::Color::YELLOW;
    } else {
      return fidl_test::Color::Unknown();
    }
  }
  
  std::string reader(fidl_test::Color color) {
    switch (color) {
      case fidl_test::Color::RED:
        return "red";
      case fidl_test::Color::BLUE:
        return "blue";
+     case fidl_test::Color::YELLOW:
+       return "yellow";
      default:
        return "<unknown>";
    }
  }

LLCPP

  • Writers can now produce instances of the new enum member.
  fidl_test::Color writer(std::string s) {
    if (s == "red") {
      return fidl_test::Color::RED;
    } else if (s == "blue") {
      return fidl_test::Color::BLUE;
+   } else if (s == "yellow") {
+     return fidl_test::Color::YELLOW;
    } else {
      return fidl_test::Color::Unknown();
    }
  }
  
  std::string reader(fidl_test::Color color) {
    switch (color) {
      case fidl_test::Color::RED:
        return "red";
      case fidl_test::Color::BLUE:
        return "blue";
+     case fidl_test::Color::YELLOW:
+       return "yellow";
      default:
        return "<unknown>";
    }
  }

Rust

  • Writers can now produce instances of the new enum member.
  • Replace the catch-all case with the unknown macro.
  fn writer(s: &str) -> fidl_lib::Color {
      match s {
          "red" => fidl_lib::Color::Red,
          "blue" => fidl_lib::Color::Blue,
+         "yellow" => fidl_lib::Color::Yellow,
          _ => fidl_lib::Color::unknown(),
      }
  }
  
  fn reader(color: fidl_lib::Color) -> &'static str {
      match color {
          fidl_lib::Color::Red => "red",
          fidl_lib::Color::Blue => "blue",
-         _ => "unknown",
+         fidl_lib::Color::Yellow => "yellow",
+         fidl_lib::ColorUnknown!() => "unknown",
      }
  }