From 5dfd0ffbdd78f8b61cccc248505bbf90028d2c30 Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Tue, 8 May 2018 17:36:51 -0400 Subject: [PATCH] Web UI: Enums IV reveng of the integer --- .vscode/c_cpp_properties.json | 2 +- src/lib/api/service_compiler/plugin.py | 8 +++-- .../service_compiler/templates/fs/fable.fs | 35 +++++++++++-------- src/web/api/__tests__/ServiceContext.test.ts | 25 +++++++------ src/web/api/src/DynamicService.ts | 32 +++++++++++++++-- src/web/api/src/bts_assert.ts | 1 + src/web/ui/src/Access.fs | 17 ++++----- src/web/ui/src/Accounts/ClipLimits.fs | 4 +-- src/web/ui/src/Accounts/State.fs | 3 +- src/web/ui/src/Accounts/Types.fs | 10 +++--- src/web/ui/src/Accounts/View.fs | 5 ++- src/web/ui/src/Controls/Fields.fs | 2 +- 12 files changed, 93 insertions(+), 51 deletions(-) diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 6272ed432..af29853f9 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -96,5 +96,5 @@ "cppStandard": "c++17" } ], - "version": 3 + "version": 4 } \ No newline at end of file diff --git a/src/lib/api/service_compiler/plugin.py b/src/lib/api/service_compiler/plugin.py index 4b367a72a..2a019710e 100755 --- a/src/lib/api/service_compiler/plugin.py +++ b/src/lib/api/service_compiler/plugin.py @@ -160,10 +160,12 @@ def to_type_model(field, msg): elif field.type == types.TYPE_ENUM: nested_enums = [x.name for x in msg.enum_type] full_name = field.type_name.lstrip('.') + fs_name = full_name short_name = full_name.split('.')[-1:][0] if short_name in nested_enums: full_name = "{0}.Types.{1}".format(msg.name, short_name) - return TypeModel(full_name, islist, nullable=full_name+'?', cpptype=short_name, jstype='number', fstype='int', natural_key=True) + fs_name = "{0}.{1}".format(msg.name, short_name) + return TypeModel(full_name, islist, nullable=full_name+'?', cpptype=short_name, jstype='number', fstype=fs_name, natural_key=True) elif field.type == types.TYPE_MESSAGE: map_types = dict([(x.name, x) for x in msg.nested_type if x.options.map_entry == True]) full_name = field.type_name.lstrip('.') @@ -226,8 +228,8 @@ def generate_insertions(params, request, response): def enum_short_name(enum_name, entry): if enum_name.lower() == entry[0:len(enum_name)].lower(): - return entry[len(enum_name)+1:] - return entry + return camel_case(entry[len(enum_name)+1:]) + return camel_case(entry) def camel_case(item): if item.isupper() and not '_' in item: return item diff --git a/src/lib/api/service_compiler/templates/fs/fable.fs b/src/lib/api/service_compiler/templates/fs/fable.fs index 9ec6d5af0..d7e579aab 100644 --- a/src/lib/api/service_compiler/templates/fs/fable.fs +++ b/src/lib/api/service_compiler/templates/fs/fable.fs @@ -7,28 +7,18 @@ open google.protobuf [] type {{name}} = {{#each values}} - | [] {{short_name}} + | [] {{short_name}} {{/each}} -[] module {{name}} = let values = [| - {{#each values}} - {{../name}}.{{short_name}}, {{number}} - {{/each}} + {{#each values}} + {{../name}}.{{short_name}} + {{/each}} |] - let valueMap = Map.ofArray values - let keyMap = Array.map (fun (a,b) -> b,a) values |> Map.ofArray - - let inline ofInt i = keyMap.[i] - let inline toInt e = valueMap.[e] - let inline toString i :string = ofInt i |> unbox - let inline ofString (s:string) : {{name}} = unbox s - let inline tryParse (s:string) = Map.tryFind (ofString s) valueMap - let inline parse s = defaultArg (tryParse s) 0 - {{/each}} + {{#each message_type}} {{#if is_empty}} type {{name}} = unit @@ -54,6 +44,21 @@ type {{name}} = {{/each}} } {{/if}} + + + {{#if nested_enum}} +// [] +module {{name}} = + {{#each nested_enum}} + [] + type {{name}} = + {{#each values}} + | [] {{short_name}} + {{/each}} + {{/each}} + {{else}} + {{/if}} + {{/each}} diff --git a/src/web/api/__tests__/ServiceContext.test.ts b/src/web/api/__tests__/ServiceContext.test.ts index 2137ab48f..9c956e3f3 100644 --- a/src/web/api/__tests__/ServiceContext.test.ts +++ b/src/web/api/__tests__/ServiceContext.test.ts @@ -1,10 +1,10 @@ import {root} from 'proto' import { connect, ServiceContext } from '../src/ServiceContext' -import { DynamicService, fillDefaults } from "../src/DynamicService" +import { DynamicService, fillDefaults, fixupEnums } from "../src/DynamicService" -test('fill defaults', () => { - let tname = "ExchangeAccountData" +test.skip('fill defaults', () => { + let tname = "SyntheticContract" const Data = root.lookupType(tname) const inst = Data.create({}) fillDefaults(tname, inst) @@ -15,23 +15,28 @@ test('log in and say hi', async () => { let ctx = await connect("bts", ""); expect(ctx instanceof ServiceContext).toBe(true) const admin = new DynamicService(ctx, "AdminService", "", [ - ["get_admin_data", 16, "bts.api.VoidMessage", "bts.api.admin.AdminData"] + ["get_admin_data", 16, "bts.api.VoidMessage", "bts.api.admin.AdminData"], + ["update_account", 1, "bts.api.admin.AccountUpdateAPIArgument", "bts.api.admin.AdminServiceOperationResult"] ]); const adminData = await admin.get_admin_data({}) - const account1 = adminData.accounts[6] - // console.log(account1) - const clip = account1.exchangeAccountData + const account1 = adminData.accounts[2] + const dat = account1.exchangeAccountData + console.log(dat) + let dattype = root.lookupType("ExchangeAccountData") + let dat2 = fixupEnums(dat, dattype) + console.log(dat2) + // const result = await admin.update_account({ completeData: account1 }) + // console.log(result.errorText) // for (let x in clip) { // console.log(x) // } // console.log(JSON.stringify( clip)) - console.log(clip) + // console.log(result) }, 15000); test.skip('api_errors_updated', () => { const ResponseHeader = root.lookupType("bts.api.AdminData") - const rh = ResponseHeader.encode({ - }).finish() + const rh = ResponseHeader.encode({ }).finish() const round = ResponseHeader.decode(rh) expect(round).toBeDefined() }); diff --git a/src/web/api/src/DynamicService.ts b/src/web/api/src/DynamicService.ts index d19d50c79..153dc2b56 100644 --- a/src/web/api/src/DynamicService.ts +++ b/src/web/api/src/DynamicService.ts @@ -8,11 +8,13 @@ export interface IService { qualifier : string } + function gen_method(service:IService, method_id : number, request_type : string, output_type : string) { const request_proto = root.lookupType(request_type) const result_proto = root.lookupType(output_type) return async arg => { const r = request_proto.create(arg) + fixupEnums(r, request_proto) assertValid(request_proto, r) const buffer = request_proto.encode(r).finish() const result = await service.context.call(service.name, service.qualifier, method_id, buffer) @@ -51,8 +53,10 @@ export function fillDefaults(ptype:string, value:any) { case "string": return value||"" default: const type = root.lookupTypeOrEnum(ptype) - const is_enum = 'valuesById' in type - if (is_enum) { return value||0; } + if (isEnum(type)) { + const intval = value||0; + return type.valuesById[intval] + } if (!value) value = {} for(const fieldname in type.fields) { const field = type.fields[fieldname] @@ -69,6 +73,30 @@ export function fillDefaults(ptype:string, value:any) { } } +function isEnum(mtype:any) { return mtype && ('valuesById' in mtype); } +// Restore string enums to integers +export function fixupEnums(message, messageType) { + if (message && messageType && messageType.fields) { + for(let fname in messageType.fields) { + let field = messageType.fields[fname] + let fieldType = field.resolvedType || root.lookup(field.type) + if (isEnum(fieldType)) { + // convert to int + message[fname] = fieldType.values[message[fname]] + } + else if (field.repeated || field.map) { + for (const key in message[fname]) { + fixupEnums(message[fname][key], fieldType) + } + } + else if (fieldType) { + fixupEnums(message[fname], fieldType) + } + } + } + return message +} + export function merge(a:T, b:object) : T { return { ... (a as any), ... b } } \ No newline at end of file diff --git a/src/web/api/src/bts_assert.ts b/src/web/api/src/bts_assert.ts index 970b089e9..09f52e54d 100644 --- a/src/web/api/src/bts_assert.ts +++ b/src/web/api/src/bts_assert.ts @@ -7,6 +7,7 @@ export function assert(condition:boolean, message?:string) { } export function assertValid(mtype, message) { const r = mtype.verify(message) + if (r != null) console.log("Validation result: " + r) assert(r == null, r) } diff --git a/src/web/ui/src/Access.fs b/src/web/ui/src/Access.fs index 23807ae33..65368b9f1 100644 --- a/src/web/ui/src/Access.fs +++ b/src/web/ui/src/Access.fs @@ -3,6 +3,7 @@ module Access open bts.api.admin open bts.api open Elmish +open bts.api.orderman let getAdminData (admin:IAdminService) (fsuc) (ffail) = Cmd.ofPromise @@ -13,18 +14,18 @@ let getAdminData (admin:IAdminService) (fsuc) (ffail) = module SecurityScope = let exchangeString ex = - match Venue.ofInt ex with + match ex with | Venue.UNSET -> "*" | x -> unbox x let securityTypeString st = - match SecurityType.ofInt st with + match st with | SecurityType.UNSET -> "*" | x -> unbox x let toSecurityScope exchange securityType symbol id = - { exchange = Venue.parse exchange - securityType = SecurityType.parse securityType + { exchange = exchange + securityType = securityType securitySymbol = symbol securityId = { id = id } } @@ -48,8 +49,8 @@ module Accounts = connections = { exchangeConnections = [||]; exchangeConnectionNames = [||]} exchangeAccountData = { cmeAccountData = - { cti = 0 - origin = 0 + { cti = CTICode.CtiCodeUndefined + origin = CustomerOrFirm.CustomerOrFirmCustomer takeupMemberCode = "" clearingAccount = "" directElectronicAccess = "" @@ -77,8 +78,8 @@ module Accounts = } let defaultSecurityScope = { - exchange = Venue.toInt Venue.GLBX - securityType = 0 + exchange = Venue.GLBX + securityType = SecurityType.FUTURE securitySymbol = "" securityId = { id = 0 } } diff --git a/src/web/ui/src/Accounts/ClipLimits.fs b/src/web/ui/src/Accounts/ClipLimits.fs index e1993a7b2..133066bef 100644 --- a/src/web/ui/src/Accounts/ClipLimits.fs +++ b/src/web/ui/src/Accounts/ClipLimits.fs @@ -16,8 +16,8 @@ let columns = let toRow index (limit:bts.api.admin.AccountSecurityClipLimitUnit) = { index = index - exchange = SecurityScope.exchangeString limit.security.exchange - security_type = SecurityScope.securityTypeString limit.security.securityType + exchange = limit.security.exchange + security_type = limit.security.securityType symbol = limit.security.securitySymbol // This one needs work security = "*" diff --git a/src/web/ui/src/Accounts/State.fs b/src/web/ui/src/Accounts/State.fs index 3e71d47f1..906f40881 100644 --- a/src/web/ui/src/Accounts/State.fs +++ b/src/web/ui/src/Accounts/State.fs @@ -27,7 +27,6 @@ let selectAccount account model = | Some a -> a } - let applyUpdate (account:BTSAccount) = function | ID id -> { account with id = id } | AccountName name -> { account with name = name } @@ -48,7 +47,7 @@ let applyCMEUpdate (data:CMEAccountData) = function let saveAccount (admin:IAdminService) (account:BTSAccount) = promise { let! result = admin.update_account { completeData = account } - if (result.isSuccess) then + if result.isSuccess then let! data = admin.get_admin_data() return UpdateAdminData data else diff --git a/src/web/ui/src/Accounts/Types.fs b/src/web/ui/src/Accounts/Types.fs index 49ad205a9..fa00a324e 100644 --- a/src/web/ui/src/Accounts/Types.fs +++ b/src/web/ui/src/Accounts/Types.fs @@ -1,6 +1,8 @@ module Accounts.Types open bts.api.admin open Fable.Core +open bts.api +open bts.api.orderman type Tab = | General @@ -12,8 +14,8 @@ type Tab = [] type ClipLimit = { index : int - exchange: string - security_type : string + exchange: Venue + security_type : SecurityType symbol : string security : string clipSize : string @@ -37,8 +39,8 @@ type AccountField = type CMEExchangeField = - | CTI of int - | Origin of int + | CTI of CTICode + | Origin of CustomerOrFirm | TakeupMember of string | ClearingAccount of string | DirectElectronicAccess of string diff --git a/src/web/ui/src/Accounts/View.fs b/src/web/ui/src/Accounts/View.fs index 8da0437e4..2946eec4a 100644 --- a/src/web/ui/src/Accounts/View.fs +++ b/src/web/ui/src/Accounts/View.fs @@ -57,7 +57,6 @@ let creditLimitsView model dispatch = div [] [str "lorem ipsum"] let exchangeSettings model dispatch = let cme = model.scratch.exchangeAccountData.cmeAccountData let send = UpdateCME>>dispatch - let inline vals x = Array.map fst x div [ClassName "columns"] [ div [ClassName "column is-narrow"] [ aside [ClassName "menu"] [ @@ -71,8 +70,8 @@ let exchangeSettings model dispatch = ] ] div [ClassName "column"] [ - Fields.enumeration "CTI" (CTICode.ofInt cme.cti) (vals CTICode.values) [] (unbox>>CTICode.toInt>>CTI>>send) - Fields.enumeration "Origin" (CustomerOrFirm.ofInt cme.origin) (vals CustomerOrFirm.values) [] (unbox>>Origin>>send) + Fields.enumeration "CTI" cme.cti CTICode.values [] (CTI>>send) + Fields.enumeration "Origin" cme.origin CustomerOrFirm.values [] (Origin>>send) Fields.text "Takeup Member Code" cme.takeupMemberCode [] (TakeupMember>>send) Fields.text "Clearing Account" cme.clearingAccount [] (ClearingAccount>>send) Fields.text "Direct Electronic Access" cme.directElectronicAccess [] (DirectElectronicAccess>>send) diff --git a/src/web/ui/src/Controls/Fields.fs b/src/web/ui/src/Controls/Fields.fs index 46fec026b..3ff5ae1a1 100644 --- a/src/web/ui/src/Controls/Fields.fs +++ b/src/web/ui/src/Controls/Fields.fs @@ -24,7 +24,7 @@ let text (lbl:string) (dv:string) (attrs:IHTMLProp list) onchange = let number (lbl:string) (dv:int) (attrs:IHTMLProp list) onchange = text lbl (string dv) attrs (int>>onchange) -let enumeration<'a> (lbl:string) (dv:'a) (values:'a[]) (attrs:IHTMLProp list) onchange = +let enumeration<'a> (lbl:string) (dv:'a) (values:'a[]) (attrs:IHTMLProp list) (onchange:'a->unit) = div [ClassName "field"] [ label [ClassName "label"] [str lbl] div [ClassName "control"] [