Skip to content

Switch from switch states to switch expressions, User from examples section #2289

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/AvroSpecific/AvroSpecific.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<AssemblyName>AvroSpecific</AssemblyName>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>7.1</LangVersion>
<LangVersion>8</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
39 changes: 15 additions & 24 deletions examples/AvroSpecific/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,12 @@ namespace Confluent.Kafka.Examples.AvroSpecific

public partial class User : ISpecificRecord
{
public static Schema _SCHEMA = Avro.Schema.Parse(@"{""type"":""record"",""name"":""User"",""namespace"":""confluent.io.examples.serialization.avro"",""fields"":[{""name"":""name"",""type"":""string""},{""name"":""favorite_number"",""type"":""long""},{""name"":""favorite_color"",""type"":""string""},{""name"":""hourly_rate"",""default"":null,""type"":[""null"",{""type"":""bytes"",""logicalType"":""decimal"",""precision"":4,""scale"":2}]}]}");
// properties could be simplified and innitializations be shortened
public virtual Schema Schema => Avro.Schema.Parse(@"{""type"":""record"",""name"":""User"",""namespace"":""confluent.io.examples.serialization.avro"",""fields"":[{""name"":""name"",""type"":""string""},{""name"":""favorite_number"",""type"":""long""},{""name"":""favorite_color"",""type"":""string""},{""name"":""hourly_rate"",""default"":null,""type"":[""null"",{""type"":""bytes"",""logicalType"":""decimal"",""precision"":4,""scale"":2}]}]}");
private string _name;
private long _favorite_number;
private string _favorite_color;
private System.Nullable<Avro.AvroDecimal> _hourly_rate;
public virtual Schema Schema
{
get
{
return User._SCHEMA;
}
}
public string name
{
get
Expand Down Expand Up @@ -71,26 +65,23 @@ public System.Nullable<Avro.AvroDecimal> hourly_rate
this._hourly_rate = value;
}
}
public virtual object Get(int fieldPos)
public virtual object Get(int fieldPos) => fieldPos switch
{
switch (fieldPos)
{
case 0: return this.name;
case 1: return this.favorite_number;
case 2: return this.favorite_color;
case 3: return this.hourly_rate;
default: throw new AvroRuntimeException("Bad index " + fieldPos + " in Get()");
};
}
0 => name,
1 => favorite_number,
2 => favorite_color,
3 => hourly_rate,
_ => throw new AvroRuntimeException("Bad index " + fieldPos + " in Get()")
};
public virtual void Put(int fieldPos, object fieldValue)
{
switch (fieldPos)
object example = fieldPos switch
{
case 0: this.name = (System.String)fieldValue; break;
case 1: this.favorite_number = (System.Int64)fieldValue; break;
case 2: this.favorite_color = (System.String)fieldValue; break;
case 3: this.hourly_rate = (System.Nullable<Avro.AvroDecimal>)fieldValue; break;
default: throw new AvroRuntimeException("Bad index " + fieldPos + " in Put()");
0 => name = (string)fieldValue,
1 => favorite_number = (Int64)fieldValue,
2 => favorite_color = (string)fieldValue,
3 => hourly_rate = (System.Nullable<Avro.AvroDecimal>)fieldValue,
_ => throw new AvroRuntimeException("Bad index " + fieldPos + " in Put()")
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<AssemblyName>AvroSpecificEncryption</AssemblyName>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>7.1</LangVersion>
<LangVersion>8</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
41 changes: 16 additions & 25 deletions examples/AvroSpecificEncryption/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// is regenerated
// </auto-generated>
// ------------------------------------------------------------------------------
namespace Confluent.Kafka.Examples.AvroSpecificEncryption
namespace Confluent.Kafka.Examples.AvroSpecific
{
using System;
using System.Collections.Generic;
Expand All @@ -15,18 +15,12 @@ namespace Confluent.Kafka.Examples.AvroSpecificEncryption

public partial class User : ISpecificRecord
{
public static Schema _SCHEMA = Avro.Schema.Parse(@"{""type"":""record"",""name"":""User"",""namespace"":""confluent.io.examples.serialization.avro"",""fields"":[{""name"":""name"",""type"":""string"",""confluent:tags"":[""PII""]},{""name"":""favorite_number"",""type"":""long""},{""name"":""favorite_color"",""type"":""string""},{""name"":""hourly_rate"",""default"":null,""type"":[""null"",{""type"":""bytes"",""logicalType"":""decimal"",""precision"":4,""scale"":2}]}]}");
// properties could be simplified and innitializations be shortened
public virtual Schema Schema => Avro.Schema.Parse(@"{""type"":""record"",""name"":""User"",""namespace"":""confluent.io.examples.serialization.avro"",""fields"":[{""name"":""name"",""type"":""string""},{""name"":""favorite_number"",""type"":""long""},{""name"":""favorite_color"",""type"":""string""},{""name"":""hourly_rate"",""default"":null,""type"":[""null"",{""type"":""bytes"",""logicalType"":""decimal"",""precision"":4,""scale"":2}]}]}");
private string _name;
private long _favorite_number;
private string _favorite_color;
private System.Nullable<Avro.AvroDecimal> _hourly_rate;
public virtual Schema Schema
{
get
{
return User._SCHEMA;
}
}
public string name
{
get
Expand Down Expand Up @@ -71,26 +65,23 @@ public System.Nullable<Avro.AvroDecimal> hourly_rate
this._hourly_rate = value;
}
}
public virtual object Get(int fieldPos)
public virtual object Get(int fieldPos) => fieldPos switch
{
switch (fieldPos)
{
case 0: return this.name;
case 1: return this.favorite_number;
case 2: return this.favorite_color;
case 3: return this.hourly_rate;
default: throw new AvroRuntimeException("Bad index " + fieldPos + " in Get()");
};
}
0 => name,
1 => favorite_number,
2 => favorite_color,
3 => hourly_rate,
_ => throw new AvroRuntimeException("Bad index " + fieldPos + " in Get()")
};
public virtual void Put(int fieldPos, object fieldValue)
{
switch (fieldPos)
object example = fieldPos switch
{
case 0: this.name = (System.String)fieldValue; break;
case 1: this.favorite_number = (System.Int64)fieldValue; break;
case 2: this.favorite_color = (System.String)fieldValue; break;
case 3: this.hourly_rate = (System.Nullable<Avro.AvroDecimal>)fieldValue; break;
default: throw new AvroRuntimeException("Bad index " + fieldPos + " in Put()");
0 => name = (string)fieldValue,
1 => favorite_number = (Int64)fieldValue,
2 => favorite_color = (string)fieldValue,
3 => hourly_rate = (System.Nullable<Avro.AvroDecimal>)fieldValue,
_ => throw new AvroRuntimeException("Bad index " + fieldPos + " in Put()")
};
}
}
Expand Down