Use Cow in favor of take.

This commit is contained in:
jeff 2021-01-12 20:44:26 +00:00
parent 6c1d19aa44
commit 69d9d7ff80
6 changed files with 52 additions and 42 deletions

View File

@ -5,14 +5,14 @@ package models.proto.cofd.api;
message CharacterIdentifier { message CharacterIdentifier {
string owner = 1; string owner = 1;
int32 character_id = 2; int32 id = 2;
} }
//Update basic information about a Chronicles of Darkness (or //Update basic information about a Chronicles of Darkness (or
//derivative system) character sheet. This is a straight overwrite of //derivative system) character sheet. This is a straight overwrite of
//all basic information on the sheet. //all basic information on the sheet.
message UpdateBasicInfoRequest { message UpdateBasicInfoRequest {
CharacterIdentifier id = 1; CharacterIdentifier character = 1;
reserved 2; reserved 2;
// string owner = 1; // string owner = 1;
// int32 character_id = 2; // int32 character_id = 2;

View File

@ -8,8 +8,8 @@ export class CharacterIdentifier extends jspb.Message {
getOwner(): string; getOwner(): string;
setOwner(value: string): void; setOwner(value: string): void;
getCharacterId(): number; getId(): number;
setCharacterId(value: number): void; setId(value: number): void;
serializeBinary(): Uint8Array; serializeBinary(): Uint8Array;
toObject(includeInstance?: boolean): CharacterIdentifier.AsObject; toObject(includeInstance?: boolean): CharacterIdentifier.AsObject;
@ -24,15 +24,15 @@ export class CharacterIdentifier extends jspb.Message {
export namespace CharacterIdentifier { export namespace CharacterIdentifier {
export type AsObject = { export type AsObject = {
owner: string, owner: string,
characterId: number, id: number,
} }
} }
export class UpdateBasicInfoRequest extends jspb.Message { export class UpdateBasicInfoRequest extends jspb.Message {
hasId(): boolean; hasCharacter(): boolean;
clearId(): void; clearCharacter(): void;
getId(): CharacterIdentifier | undefined; getCharacter(): CharacterIdentifier | undefined;
setId(value?: CharacterIdentifier): void; setCharacter(value?: CharacterIdentifier): void;
getName(): string; getName(): string;
setName(value: string): void; setName(value: string): void;
@ -61,7 +61,7 @@ export class UpdateBasicInfoRequest extends jspb.Message {
export namespace UpdateBasicInfoRequest { export namespace UpdateBasicInfoRequest {
export type AsObject = { export type AsObject = {
id?: CharacterIdentifier.AsObject, character?: CharacterIdentifier.AsObject,
name: string, name: string,
gender: string, gender: string,
concept: string, concept: string,

View File

@ -247,7 +247,7 @@ proto.models.proto.cofd.api.CharacterIdentifier.prototype.toObject = function(op
proto.models.proto.cofd.api.CharacterIdentifier.toObject = function(includeInstance, msg) { proto.models.proto.cofd.api.CharacterIdentifier.toObject = function(includeInstance, msg) {
var f, obj = { var f, obj = {
owner: jspb.Message.getFieldWithDefault(msg, 1, ""), owner: jspb.Message.getFieldWithDefault(msg, 1, ""),
characterId: jspb.Message.getFieldWithDefault(msg, 2, 0) id: jspb.Message.getFieldWithDefault(msg, 2, 0)
}; };
if (includeInstance) { if (includeInstance) {
@ -290,7 +290,7 @@ proto.models.proto.cofd.api.CharacterIdentifier.deserializeBinaryFromReader = fu
break; break;
case 2: case 2:
var value = /** @type {number} */ (reader.readInt32()); var value = /** @type {number} */ (reader.readInt32());
msg.setCharacterId(value); msg.setId(value);
break; break;
default: default:
reader.skipField(); reader.skipField();
@ -328,7 +328,7 @@ proto.models.proto.cofd.api.CharacterIdentifier.serializeBinaryToWriter = functi
f f
); );
} }
f = message.getCharacterId(); f = message.getId();
if (f !== 0) { if (f !== 0) {
writer.writeInt32( writer.writeInt32(
2, 2,
@ -357,10 +357,10 @@ proto.models.proto.cofd.api.CharacterIdentifier.prototype.setOwner = function(va
/** /**
* optional int32 character_id = 2; * optional int32 id = 2;
* @return {number} * @return {number}
*/ */
proto.models.proto.cofd.api.CharacterIdentifier.prototype.getCharacterId = function() { proto.models.proto.cofd.api.CharacterIdentifier.prototype.getId = function() {
return /** @type {number} */ (jspb.Message.getFieldWithDefault(this, 2, 0)); return /** @type {number} */ (jspb.Message.getFieldWithDefault(this, 2, 0));
}; };
@ -369,7 +369,7 @@ proto.models.proto.cofd.api.CharacterIdentifier.prototype.getCharacterId = funct
* @param {number} value * @param {number} value
* @return {!proto.models.proto.cofd.api.CharacterIdentifier} returns this * @return {!proto.models.proto.cofd.api.CharacterIdentifier} returns this
*/ */
proto.models.proto.cofd.api.CharacterIdentifier.prototype.setCharacterId = function(value) { proto.models.proto.cofd.api.CharacterIdentifier.prototype.setId = function(value) {
return jspb.Message.setProto3IntField(this, 2, value); return jspb.Message.setProto3IntField(this, 2, value);
}; };
@ -406,7 +406,7 @@ proto.models.proto.cofd.api.UpdateBasicInfoRequest.prototype.toObject = function
*/ */
proto.models.proto.cofd.api.UpdateBasicInfoRequest.toObject = function(includeInstance, msg) { proto.models.proto.cofd.api.UpdateBasicInfoRequest.toObject = function(includeInstance, msg) {
var f, obj = { var f, obj = {
id: (f = msg.getId()) && proto.models.proto.cofd.api.CharacterIdentifier.toObject(includeInstance, f), character: (f = msg.getCharacter()) && proto.models.proto.cofd.api.CharacterIdentifier.toObject(includeInstance, f),
name: jspb.Message.getFieldWithDefault(msg, 3, ""), name: jspb.Message.getFieldWithDefault(msg, 3, ""),
gender: jspb.Message.getFieldWithDefault(msg, 4, ""), gender: jspb.Message.getFieldWithDefault(msg, 4, ""),
concept: jspb.Message.getFieldWithDefault(msg, 5, ""), concept: jspb.Message.getFieldWithDefault(msg, 5, ""),
@ -451,7 +451,7 @@ proto.models.proto.cofd.api.UpdateBasicInfoRequest.deserializeBinaryFromReader =
case 1: case 1:
var value = new proto.models.proto.cofd.api.CharacterIdentifier; var value = new proto.models.proto.cofd.api.CharacterIdentifier;
reader.readMessage(value,proto.models.proto.cofd.api.CharacterIdentifier.deserializeBinaryFromReader); reader.readMessage(value,proto.models.proto.cofd.api.CharacterIdentifier.deserializeBinaryFromReader);
msg.setId(value); msg.setCharacter(value);
break; break;
case 3: case 3:
var value = /** @type {string} */ (reader.readString()); var value = /** @type {string} */ (reader.readString());
@ -502,7 +502,7 @@ proto.models.proto.cofd.api.UpdateBasicInfoRequest.prototype.serializeBinary = f
*/ */
proto.models.proto.cofd.api.UpdateBasicInfoRequest.serializeBinaryToWriter = function(message, writer) { proto.models.proto.cofd.api.UpdateBasicInfoRequest.serializeBinaryToWriter = function(message, writer) {
var f = undefined; var f = undefined;
f = message.getId(); f = message.getCharacter();
if (f != null) { if (f != null) {
writer.writeMessage( writer.writeMessage(
1, 1,
@ -549,10 +549,10 @@ proto.models.proto.cofd.api.UpdateBasicInfoRequest.serializeBinaryToWriter = fun
/** /**
* optional CharacterIdentifier id = 1; * optional CharacterIdentifier character = 1;
* @return {?proto.models.proto.cofd.api.CharacterIdentifier} * @return {?proto.models.proto.cofd.api.CharacterIdentifier}
*/ */
proto.models.proto.cofd.api.UpdateBasicInfoRequest.prototype.getId = function() { proto.models.proto.cofd.api.UpdateBasicInfoRequest.prototype.getCharacter = function() {
return /** @type{?proto.models.proto.cofd.api.CharacterIdentifier} */ ( return /** @type{?proto.models.proto.cofd.api.CharacterIdentifier} */ (
jspb.Message.getWrapperField(this, proto.models.proto.cofd.api.CharacterIdentifier, 1)); jspb.Message.getWrapperField(this, proto.models.proto.cofd.api.CharacterIdentifier, 1));
}; };
@ -562,7 +562,7 @@ proto.models.proto.cofd.api.UpdateBasicInfoRequest.prototype.getId = function()
* @param {?proto.models.proto.cofd.api.CharacterIdentifier|undefined} value * @param {?proto.models.proto.cofd.api.CharacterIdentifier|undefined} value
* @return {!proto.models.proto.cofd.api.UpdateBasicInfoRequest} returns this * @return {!proto.models.proto.cofd.api.UpdateBasicInfoRequest} returns this
*/ */
proto.models.proto.cofd.api.UpdateBasicInfoRequest.prototype.setId = function(value) { proto.models.proto.cofd.api.UpdateBasicInfoRequest.prototype.setCharacter = function(value) {
return jspb.Message.setWrapperField(this, 1, value); return jspb.Message.setWrapperField(this, 1, value);
}; };
@ -571,8 +571,8 @@ proto.models.proto.cofd.api.UpdateBasicInfoRequest.prototype.setId = function(va
* Clears the message field making it undefined. * Clears the message field making it undefined.
* @return {!proto.models.proto.cofd.api.UpdateBasicInfoRequest} returns this * @return {!proto.models.proto.cofd.api.UpdateBasicInfoRequest} returns this
*/ */
proto.models.proto.cofd.api.UpdateBasicInfoRequest.prototype.clearId = function() { proto.models.proto.cofd.api.UpdateBasicInfoRequest.prototype.clearCharacter = function() {
return this.setId(undefined); return this.setCharacter(undefined);
}; };
@ -580,7 +580,7 @@ proto.models.proto.cofd.api.UpdateBasicInfoRequest.prototype.clearId = function(
* Returns whether this field is set. * Returns whether this field is set.
* @return {boolean} * @return {boolean}
*/ */
proto.models.proto.cofd.api.UpdateBasicInfoRequest.prototype.hasId = function() { proto.models.proto.cofd.api.UpdateBasicInfoRequest.prototype.hasCharacter = function() {
return jspb.Message.getField(this, 1) != null; return jspb.Message.getField(this, 1) != null;
}; };

View File

@ -12,7 +12,7 @@ import * as api from "../api";
function characterId(): CharacterIdentifier { function characterId(): CharacterIdentifier {
const id = new CharacterIdentifier(); const id = new CharacterIdentifier();
id.setCharacterId(parseInt(CHARACTER_ID)); id.setId(parseInt(CHARACTER_ID));
id.setOwner(USERNAME); id.setOwner(USERNAME);
return id; return id;
} }
@ -77,7 +77,7 @@ import * as api from "../api";
async function updateInfo() { async function updateInfo() {
const params = new UpdateBasicInfoRequest(); const params = new UpdateBasicInfoRequest();
params.setId(characterId()); params.setCharacter(characterId());
params.setName(getTextValue("#characterName")); params.setName(getTextValue("#characterName"));
params.setAge(getIntValue("#age")); params.setAge(getIntValue("#age"));
params.setConcept(getTextValue("#concept")); params.setConcept(getTextValue("#concept"));

View File

@ -14,21 +14,30 @@ pub mod api {
//include!(concat!(env!("OUT_DIR"), "/models.proto.cofd.api.rs")); //include!(concat!(env!("OUT_DIR"), "/models.proto.cofd.api.rs"));
tonic::include_proto!("models.proto.cofd.api"); tonic::include_proto!("models.proto.cofd.api");
/// Trait to take ownership of CharacterIdentifiers while avoiding use std::borrow::Cow;
/// clone. The identifier is extracted out of the type, leaving a
/// default value in its place. /// Trait to extract values out of a CharacterIdentifier whose
/// instance may or may not exist on an API request.
pub trait DefaultCharacterIdentifier { pub trait DefaultCharacterIdentifier {
/// Take ownership of a CharacterIdentifier by taking it out /// Retrieve the specified owner, or a default fallback (empty
/// of the containing type, leaving behind a default value /// string). Will not allocate if the owner is present in the
/// (None, in the case of Option) in its place. /// request.
fn extract(&mut self) -> CharacterIdentifier; fn owner(&self) -> Cow<'_, str>;
/// Retrieve the character ID specified, or the default i32
/// value (0).
fn id(&self) -> i32;
} }
impl DefaultCharacterIdentifier for Option<CharacterIdentifier> { impl DefaultCharacterIdentifier for Option<CharacterIdentifier> {
/// Extract the identifier, or a default one if the identifier fn owner(&self) -> Cow<'_, str> {
/// is not specified for some reason. self.as_ref()
fn extract(&mut self) -> CharacterIdentifier { .map(|ident| Cow::from(&ident.owner))
self.take().unwrap_or_default() .unwrap_or_default()
}
fn id(&self) -> i32 {
self.as_ref().map(|ident| ident.id).unwrap_or_default()
} }
} }

View File

@ -40,12 +40,13 @@ fn find_skill<'a>(sheet: &'a mut CofdSheet, skill_name: &'a str) -> Option<&'a m
#[post("/rpc/cofd/update_basic_info", data = "<req>")] #[post("/rpc/cofd/update_basic_info", data = "<req>")]
pub(super) async fn update_basic_info<'a>( pub(super) async fn update_basic_info<'a>(
mut req: Proto<UpdateBasicInfoRequest>, req: Proto<UpdateBasicInfoRequest>,
conn: TenebrousDbConn<'_>, conn: TenebrousDbConn<'_>,
logged_in_user: Option<&User>, user: Option<&User>,
) -> Result<Proto<ApiResult>, Error> { ) -> Result<Proto<ApiResult>, Error> {
let id = req.id.extract(); let mut character =
let mut character = load_character(&conn, logged_in_user, &id.owner, id.character_id).await?; load_character(&conn, user, &req.character.owner(), req.character.id()).await?;
let mut sheet: CofdSheet = character.try_deserialize()?; let mut sheet: CofdSheet = character.try_deserialize()?;
println!("name will now be {}", req.name); println!("name will now be {}", req.name);