tree: dd09de1ae206d41adf0a20ba032983ae77db090a [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/union-member-add/README.md

Add union member

Overview

-initstep 1step 2step 3
fidllinklink
dartlinklinklink
golinklinklink
hlcpplinklinklink
llcpplinklinklink
rustlinklinklink

Initial State

FIDL

flexible union JsonValue {
    1: int32 int_value;
    2: string:MAX string_value;
};

Dart

fidllib.JsonValue writer(String s) {
  final asInt = int.tryParse(s);
  if (asInt != null) {
    return fidllib.JsonValue.withIntValue(asInt);
  }
  return fidllib.JsonValue.withStringValue(s);
}

String reader(fidllib.JsonValue value) {
  switch (value.$tag) {
    case fidllib.JsonValueTag.intValue:
      return '${value.intValue}';
    case fidllib.JsonValueTag.stringValue:
      return value.stringValue;
    default:
      return '<${value.$unknownData.data.length} unknown bytes>';
  }
}

Go

func writer(s string) lib.JsonValue {
	n, err := strconv.ParseInt(s, 10, 32)
	if err == nil {
		return lib.JsonValueWithIntValue(int32(n))
	}
	return lib.JsonValueWithStringValue(s)
}

func reader(value lib.JsonValue) string {
	switch value.Which() {
	case lib.JsonValueIntValue:
		return fmt.Sprintf("%d", value.IntValue)
	case lib.JsonValueStringValue:
		return value.StringValue
	default:
		return fmt.Sprintf("<%d unknown bytes>", len(value.GetUnknownData().Bytes))
	}
}

HLCPP

fidl_test::JsonValue writer(const std::string& s) {
  std::optional<int32_t> maybe_int = parse_as_int(s);
  if (maybe_int) {
    return fidl_test::JsonValue::WithIntValue(std::move(*maybe_int));
  }
  auto val = s;
  return fidl_test::JsonValue::WithStringValue(std::move(val));
}

std::string reader(const fidl_test::JsonValue& value) {
  switch (value.Which()) {
    case fidl_test::JsonValue::Tag::kIntValue:
      return std::to_string(value.int_value());
    case fidl_test::JsonValue::Tag::kStringValue:
      return value.string_value();
    case fidl_test::JsonValue::Tag::Invalid:
      return "<uninitialized>";
    case fidl_test::JsonValue::Tag::kUnknown: {
      std::ostringstream out;
      out << "<" << value.UnknownBytes()->size() << " unknown bytes>";
      return out.str();
    }
  }
}

LLCPP

fidl_test::JsonValue writer(const std::string& s) {
  std::optional<int32_t> maybe_int = parse_as_int(s);
  if (maybe_int) {
    return fidl_test::JsonValue::WithIntValue(std::make_unique<int32_t>(*maybe_int));
  }
  return fidl_test::JsonValue::WithStringValue(
      std::make_unique<fidl::StringView>(fidl::heap_copy_str(s)));
}

std::string reader(const fidl_test::JsonValue& value) {
  switch (value.which()) {
    case fidl_test::JsonValue::Tag::kIntValue:
      return std::to_string(value.int_value());
    case fidl_test::JsonValue::Tag::kStringValue:
      return std::string(value.string_value().data(), value.string_value().size());
    case fidl_test::JsonValue::Tag::kUnknown:
      return "<unknown>";
  }
}

Rust

fn writer(s: &str) -> fidl_lib::JsonValue {
    let as_int = s.parse::<i32>();
    as_int
        .map(|n| fidl_lib::JsonValue::IntValue(n))
        .unwrap_or(fidl_lib::JsonValue::StringValue(s.to_string()))
}

fn reader(value: fidl_lib::JsonValue) -> String {
    match value {
        fidl_lib::JsonValue::IntValue(n) => format!("{}", n),
        fidl_lib::JsonValue::StringValue(s) => s,
        fidl_lib::JsonValueUnknown!() => "<unknown>".to_string(),
    }
}

Update Source Code

Dart

  • Evolve readers first by updating switch statements on the union tag to handle the soon-to-be-added variant as part of a default case.
    if (asInt != null) {
      return fidllib.JsonValue.withIntValue(asInt);
    }
    return fidllib.JsonValue.withStringValue(s);
  }
  
  String reader(fidllib.JsonValue value) {
    switch (value.$tag) {
      case fidllib.JsonValueTag.intValue:
        return '${value.intValue}';
      case fidllib.JsonValueTag.stringValue:
        return value.stringValue;
+     case fidllib.JsonValueTag.$unknown:
+       return '<${value.$unknownData.data.length} unknown bytes>';
      default:
-       return '<${value.$unknownData.data.length} unknown bytes>';
+       return '<unknown new member>';
    }
  }
  

Go

  • Evolve readers first by updating switch statements on the union tag to handle the soon-to-be-added variant as part of a default case.
  	if err == nil {
  		return lib.JsonValueWithIntValue(int32(n))
  	}
  	return lib.JsonValueWithStringValue(s)
  }
  
  func reader(value lib.JsonValue) string {
  	switch value.Which() {
  	case lib.JsonValueIntValue:
  		return fmt.Sprintf("%d", value.IntValue)
  	case lib.JsonValueStringValue:
  		return value.StringValue
+ 	case lib.JsonValue_unknownData:
+ 		return fmt.Sprintf("<%d unknown bytes>", len(value.GetUnknownData().Bytes))
  	default:
- 		return fmt.Sprintf("<%d unknown bytes>", len(value.GetUnknownData().Bytes))
+ 		return "<unknown new member>"
  	}
  }
  

HLCPP

  • Evolve readers first by updating switch statements on the union tag to handle the soon-to-be-added variant as part of a default case.
    switch (value.Which()) {
      case fidl_test::JsonValue::Tag::kIntValue:
        return std::to_string(value.int_value());
      case fidl_test::JsonValue::Tag::kStringValue:
        return value.string_value();
      case fidl_test::JsonValue::Tag::Invalid:
        return "<uninitialized>";
      case fidl_test::JsonValue::Tag::kUnknown: {
        std::ostringstream out;
        out << "<" << value.UnknownBytes()->size() << " unknown bytes>";
        return out.str();
      }
+     default:
+       return "<unknown new member>";
    }
  }

LLCPP

  • Evolve readers first by updating switch statements on the union tag to handle the soon-to-be-added variant as part of a default case.
    }
    return fidl_test::JsonValue::WithStringValue(
        std::make_unique<fidl::StringView>(fidl::heap_copy_str(s)));
  }
  
  std::string reader(const fidl_test::JsonValue& value) {
    switch (value.which()) {
      case fidl_test::JsonValue::Tag::kIntValue:
        return std::to_string(value.int_value());
      case fidl_test::JsonValue::Tag::kStringValue:
        return std::string(value.string_value().data(), value.string_value().size());
      case fidl_test::JsonValue::Tag::kUnknown:
+     default:
        return "<unknown>";
    }
  }

Rust

  • Evolve readers first by updating match statements to handle the soon-to-be-added variant as part of a catch-all case.
  fn writer(s: &str) -> fidl_lib::JsonValue {
      let as_int = s.parse::<i32>();
      as_int
          .map(|n| fidl_lib::JsonValue::IntValue(n))
          .unwrap_or(fidl_lib::JsonValue::StringValue(s.to_string()))
  }
  
  fn reader(value: fidl_lib::JsonValue) -> String {
      match value {
          fidl_lib::JsonValue::IntValue(n) => format!("{}", n),
          fidl_lib::JsonValue::StringValue(s) => s,
-         fidl_lib::JsonValueUnknown!() => "<unknown>".to_string(),
+         _ => "<unknown>".to_string(),
      }
  }

Update FIDL Library

  • Add the new variant
  flexible union JsonValue {
      1: int32 int_value;
      2: string:MAX string_value;
+     3: float32 float_value;
  };

Update Source Code

Dart

  • Writers can now start producing instances of the new variant.
  • Readers should handle the new variant in switch statements.
  fidllib.JsonValue writer(String s) {
+   final asFloat = double.tryParse(s);
+   if (asFloat != null) {
+     return fidllib.JsonValue.withFloatValue(asFloat);
+   }
    final asInt = int.tryParse(s);
    if (asInt != null) {
      return fidllib.JsonValue.withIntValue(asInt);
    }
    return fidllib.JsonValue.withStringValue(s);
  }
  
  String reader(fidllib.JsonValue value) {
    switch (value.$tag) {
      case fidllib.JsonValueTag.intValue:
        return '${value.intValue}';
      case fidllib.JsonValueTag.stringValue:
        return value.stringValue;
-     case fidllib.JsonValueTag.$unknown:
+     case fidllib.JsonValueTag.floatValue:
+       return '${value.floatValue}';
+     default:
        return '<${value.$unknownData.data.length} unknown bytes>';
-     default:
-       return '<unknown new member>';
    }
  }
  

Go

  • Writers can now start producing instances of the new variant.
  • Readers should handle the new variant in switch statements.
  func writer(s string) lib.JsonValue {
  	n, err := strconv.ParseInt(s, 10, 32)
  	if err == nil {
  		return lib.JsonValueWithIntValue(int32(n))
+ 	}
+ 	var f float64
+ 	f, err = strconv.ParseFloat(s, 64)
+ 	if err == nil {
+ 		return lib.JsonValueWithFloatValue(float32(f))
  	}
  	return lib.JsonValueWithStringValue(s)
  }
  
  func reader(value lib.JsonValue) string {
  	switch value.Which() {
  	case lib.JsonValueIntValue:
  		return fmt.Sprintf("%d", value.IntValue)
  	case lib.JsonValueStringValue:
  		return value.StringValue
- 	case lib.JsonValue_unknownData:
+ 	case lib.JsonValueFloatValue:
+ 		return fmt.Sprintf("%f", value.FloatValue)
+ 	default:
  		return fmt.Sprintf("<%d unknown bytes>", len(value.GetUnknownData().Bytes))
- 	default:
- 		return "<unknown new member>"
  	}
  }
  

HLCPP

  • Writers can now start producing instances of the new variant.
  • Readers can replace the default case by handling the new variant directly.
  fidl_test::JsonValue writer(const std::string& s) {
+   std::optional<float> maybe_float = parse_as_float(s);
+   if (maybe_float) {
+     return fidl_test::JsonValue::WithFloatValue(std::move(*maybe_float));
+   }
+ 
    std::optional<int32_t> maybe_int = parse_as_int(s);
    if (maybe_int) {
      return fidl_test::JsonValue::WithIntValue(std::move(*maybe_int));
    }
    auto val = s;
    return fidl_test::JsonValue::WithStringValue(std::move(val));
  }
  
  std::string reader(const fidl_test::JsonValue& value) {
    switch (value.Which()) {
      case fidl_test::JsonValue::Tag::kIntValue:
        return std::to_string(value.int_value());
      case fidl_test::JsonValue::Tag::kStringValue:
        return value.string_value();
+     case fidl_test::JsonValue::Tag::kFloatValue:
+       return std::to_string(value.float_value());
      case fidl_test::JsonValue::Tag::Invalid:
        return "<uninitialized>";
      case fidl_test::JsonValue::Tag::kUnknown: {
        std::ostringstream out;
        out << "<" << value.UnknownBytes()->size() << " unknown bytes>";
        return out.str();
-     }
-     default:
-       return "<unknown new member>";
+     };
    }
  }

LLCPP

  • Writers can now start producing instances of the new variant.
  • Readers can replace the default case by handling the new variant directly.
  fidl_test::JsonValue writer(const std::string& s) {
+   std::optional<float> maybe_float = parse_as_float(s);
+   ;
+   if (maybe_float) {
+     return fidl_test::JsonValue::WithIntValue(std::make_unique<int32_t>(*maybe_float));
+   }
    std::optional<int32_t> maybe_int = parse_as_int(s);
    if (maybe_int) {
      return fidl_test::JsonValue::WithIntValue(std::make_unique<int32_t>(*maybe_int));
    }
    return fidl_test::JsonValue::WithStringValue(
        std::make_unique<fidl::StringView>(fidl::heap_copy_str(s)));
  }
  
  std::string reader(const fidl_test::JsonValue& value) {
    switch (value.which()) {
      case fidl_test::JsonValue::Tag::kIntValue:
        return std::to_string(value.int_value());
      case fidl_test::JsonValue::Tag::kStringValue:
        return std::string(value.string_value().data(), value.string_value().size());
+     case fidl_test::JsonValue::Tag::kFloatValue:
+       return std::to_string(value.float_value());
      case fidl_test::JsonValue::Tag::kUnknown:
-     default:
        return "<unknown>";
    }
  }

Rust

  • Writers can now start producing instances of the new variant.
  • Readers can replace the default case by handling the new variant directly.
  fn writer(s: &str) -> fidl_lib::JsonValue {
+     let as_float = s.parse::<f32>();
+     if let Ok(val) = as_float {
+         return fidl_lib::JsonValue::FloatValue(val);
+     }
      let as_int = s.parse::<i32>();
      as_int
          .map(|n| fidl_lib::JsonValue::IntValue(n))
          .unwrap_or(fidl_lib::JsonValue::StringValue(s.to_string()))
  }
  
  fn reader(value: fidl_lib::JsonValue) -> String {
      match value {
          fidl_lib::JsonValue::IntValue(n) => format!("{}", n),
          fidl_lib::JsonValue::StringValue(s) => s,
-         _ => "<unknown>".to_string(),
+         fidl_lib::JsonValue::FloatValue(v) => format!("{:.2}", v),
+         fidl_lib::JsonValueUnknown!() => "<unknown>".to_string(),
      }
  }