SafeCastOverVariant.yql 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. (
  2. (let config (DataSource 'config))
  3. (let world (Configure! world config 'PureDataSource 'yt))
  4. (let res_sink (DataSink 'result))
  5. ### Tuple
  6. (let srcType (VariantType (TupleType (DataType 'Int32) (DataType 'String))))
  7. (let targetType (VariantType (TupleType (DataType 'Uint8) (DataType 'Utf8))))
  8. # Variant<Tuple<Int32, String>> -> Variant<Tuple<Uint8, Utf8>>? (good)
  9. (let value (Variant (String 'one) '1 srcType))
  10. (let cast (SafeCast value targetType))
  11. (let world (Write! world res_sink (Key) cast '('('type))))
  12. # Variant<Tuple<Int32, String>> -> Variant<Tuple<Uint8, Utf8>>? (null)
  13. (let value (Variant (String '"garbage\xff") '1 srcType))
  14. (let cast (SafeCast value targetType))
  15. (let world (Write! world res_sink (Key) cast '('('type))))
  16. ### Struct
  17. (let srcType (VariantType (StructType '('a (DataType 'Int32)) '('b (DataType 'String)))))
  18. (let targetType (VariantType (StructType '('a (DataType 'Uint8)) '('b (DataType 'Utf8)))))
  19. # Variant<Struct<Int32, String>> -> Variant<Struct<Uint8, Utf8>>? (good)
  20. (let value (Variant (Int32 '1) 'a srcType))
  21. (let cast (SafeCast value targetType))
  22. (let world (Write! world res_sink (Key) cast '('('type))))
  23. # Variant<Struct<Int32, String>> -> Variant<Struct<Uint8, Utf8>>? (null)
  24. (let value (Variant (Int32 '"-1") 'a srcType))
  25. (let cast (SafeCast value targetType))
  26. (let world (Write! world res_sink (Key) cast '('('type))))
  27. (let world (Commit! world res_sink))
  28. (return world)
  29. )