Use Cow in favor of take.

This commit is contained in:
projectmoon 2021-01-12 20:44:26 +00:00
parent a36e8069f4
commit 3352c2067f
6 changed files with 52 additions and 42 deletions

View File

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

View File

@ -8,8 +8,8 @@ export class CharacterIdentifier extends jspb.Message {
getOwner(): string;
setOwner(value: string): void;
getCharacterId(): number;
setCharacterId(value: number): void;
getId(): number;
setId(value: number): void;
serializeBinary(): Uint8Array;
toObject(includeInstance?: boolean): CharacterIdentifier.AsObject;
@ -24,15 +24,15 @@ export class CharacterIdentifier extends jspb.Message {
export namespace CharacterIdentifier {
export type AsObject = {
owner: string,
characterId: number,
id: number,
}
}
export class UpdateBasicInfoRequest extends jspb.Message {
hasId(): boolean;
clearId(): void;
getId(): CharacterIdentifier | undefined;
setId(value?: CharacterIdentifier): void;
hasCharacter(): boolean;
clearCharacter(): void;
getCharacter(): CharacterIdentifier | undefined;
setCharacter(value?: CharacterIdentifier): void;
getName(): string;
setName(value: string): void;
@ -61,7 +61,7 @@ export class UpdateBasicInfoRequest extends jspb.Message {
export namespace UpdateBasicInfoRequest {
export type AsObject = {
id?: CharacterIdentifier.AsObject,
character?: CharacterIdentifier.AsObject,
name: string,
gender: 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) {
var f, obj = {
owner: jspb.Message.getFieldWithDefault(msg, 1, ""),
characterId: jspb.Message.getFieldWithDefault(msg, 2, 0)
id: jspb.Message.getFieldWithDefault(msg, 2, 0)
};
if (includeInstance) {
@ -290,7 +290,7 @@ proto.models.proto.cofd.api.CharacterIdentifier.deserializeBinaryFromReader = fu
break;
case 2:
var value = /** @type {number} */ (reader.readInt32());
msg.setCharacterId(value);
msg.setId(value);
break;
default:
reader.skipField();
@ -328,7 +328,7 @@ proto.models.proto.cofd.api.CharacterIdentifier.serializeBinaryToWriter = functi
f
);
}
f = message.getCharacterId();
f = message.getId();
if (f !== 0) {
writer.writeInt32(
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}
*/
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));
};
@ -369,7 +369,7 @@ proto.models.proto.cofd.api.CharacterIdentifier.prototype.getCharacterId = funct
* @param {number} value
* @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);
};
@ -406,7 +406,7 @@ proto.models.proto.cofd.api.UpdateBasicInfoRequest.prototype.toObject = function
*/
proto.models.proto.cofd.api.UpdateBasicInfoRequest.toObject = function(includeInstance, msg) {
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, ""),
gender: jspb.Message.getFieldWithDefault(msg, 4, ""),
concept: jspb.Message.getFieldWithDefault(msg, 5, ""),
@ -451,7 +451,7 @@ proto.models.proto.cofd.api.UpdateBasicInfoRequest.deserializeBinaryFromReader =
case 1:
var value = new proto.models.proto.cofd.api.CharacterIdentifier;
reader.readMessage(value,proto.models.proto.cofd.api.CharacterIdentifier.deserializeBinaryFromReader);
msg.setId(value);
msg.setCharacter(value);
break;
case 3:
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) {
var f = undefined;
f = message.getId();
f = message.getCharacter();
if (f != null) {
writer.writeMessage(
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}
*/
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} */ (
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
* @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);
};
@ -571,8 +571,8 @@ proto.models.proto.cofd.api.UpdateBasicInfoRequest.prototype.setId = function(va
* Clears the message field making it undefined.
* @return {!proto.models.proto.cofd.api.UpdateBasicInfoRequest} returns this
*/
proto.models.proto.cofd.api.UpdateBasicInfoRequest.prototype.clearId = function() {
return this.setId(undefined);
proto.models.proto.cofd.api.UpdateBasicInfoRequest.prototype.clearCharacter = function() {
return this.setCharacter(undefined);
};
@ -580,7 +580,7 @@ proto.models.proto.cofd.api.UpdateBasicInfoRequest.prototype.clearId = function(
* Returns whether this field is set.
* @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;
};

View File

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

View File

@ -14,21 +14,30 @@ pub mod api {
//include!(concat!(env!("OUT_DIR"), "/models.proto.cofd.api.rs"));
tonic::include_proto!("models.proto.cofd.api");
/// Trait to take ownership of CharacterIdentifiers while avoiding
/// clone. The identifier is extracted out of the type, leaving a
/// default value in its place.
use std::borrow::Cow;
/// Trait to extract values out of a CharacterIdentifier whose
/// instance may or may not exist on an API request.
pub trait DefaultCharacterIdentifier {
/// Take ownership of a CharacterIdentifier by taking it out
/// of the containing type, leaving behind a default value
/// (None, in the case of Option) in its place.
fn extract(&mut self) -> CharacterIdentifier;
/// Retrieve the specified owner, or a default fallback (empty
/// string). Will not allocate if the owner is present in the
/// request.
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> {
/// Extract the identifier, or a default one if the identifier
/// is not specified for some reason.
fn extract(&mut self) -> CharacterIdentifier {
self.take().unwrap_or_default()
fn owner(&self) -> Cow<'_, str> {
self.as_ref()
.map(|ident| Cow::from(&ident.owner))
.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>")]
pub(super) async fn update_basic_info<'a>(
mut req: Proto<UpdateBasicInfoRequest>,
req: Proto<UpdateBasicInfoRequest>,
conn: TenebrousDbConn<'_>,
logged_in_user: Option<&User>,
user: Option<&User>,
) -> Result<Proto<ApiResult>, Error> {
let id = req.id.extract();
let mut character = load_character(&conn, logged_in_user, &id.owner, id.character_id).await?;
let mut character =
load_character(&conn, user, &req.character.owner(), req.character.id()).await?;
let mut sheet: CofdSheet = character.try_deserialize()?;
println!("name will now be {}", req.name);