Web UI: Enums IV reveng of the integer

This commit is contained in:
Eric Harding committed 2018-05-08 22:18:33 -04:00
1 parent 12e4e3ad8d
commit 5dfd0ffbdd
12 files changed
+93 -51

No files matched your search

+1 -1
View File
@@ -96,5 +96,5 @@
"cppStandard": "c++17"
}
],
"version": 3
"version": 4
}
+5 -3
View File
@@ -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
@@ -7,28 +7,18 @@ open google.protobuf
[<StringEnum>]
type {{name}} =
{{#each values}}
| [<CompiledName("{{short_name}}")>] {{short_name}}
| [<CompiledName("{{name}}")>] {{short_name}}
{{/each}}
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
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}}
// [<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module {{name}} =
{{#each nested_enum}}
[<StringEnum>]
type {{name}} =
{{#each values}}
| [<CompiledName("{{name}}")>] {{short_name}}
{{/each}}
{{/each}}
{{else}}
{{/if}}
{{/each}}
+15 -10
View File
@@ -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(<ServiceContext>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()
});
+30 -2
View File
@@ -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<T>(a:T, b:object) : T {
return { ... (a as any), ... b }
}
+1
View File
@@ -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)
}
+9 -8
View File
@@ -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 }
}
+2 -2
View File
@@ -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 = "*"
+1 -2
View File
@@ -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
+6 -4
View File
@@ -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 =
[<Pojo>]
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
+2 -3
View File
@@ -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)
+1 -1
View File
@@ -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"] [