Skip to content
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

Take advantage of ICustomFormatter or IFormattable when an object with default destructuring #23

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

lust4life
Copy link

@lust4life lust4life commented Nov 3, 2017

Change scalarStringCatchAllDestr to catch the original scalar object, then format properly when writePropValue.

e.g.

type User = 
  {
    id      : int 
    name    : string
    created : DateTime
  }
with
  interface IFormattable with
    member x.ToString (format, provider) =
      sprintf "id => %i, name => %s, created => %s" x.id x.name (x.created.ToShortDateString())

let foo = { id = 999; name = "foo"; created = DateTime.Now}
let nl = Environment.NewLine + Environment.NewLine

Formatting.format (Parser.parse "{default} {nl} {$stringify} {nl} {@capture}") [| foo; nl; foo; nl; foo; |] 
System.String.Format("{0}",foo)

before:

> Formatting.format (Parser.parse "{default} {nl} {$stringify} {nl} {@capture}") [| foo; nl; foo; nl; foo; |]
- ;;
val it : string =
  ""{id = 999;
 name = \"foo\";
 created = 11/3/2017 7:49:41 PM;}" "

" "{id = 999;
 name = \"foo\";
 created = 11/3/2017 7:49:41 PM;}" "

" User { id: 999, name: "foo", created: 11/3/2017 7:49:41 PM }"

> System.String.Format("{0}",foo)
- ;;
val it : string = "id => 999, name => foo, created => 11/3/2017"

after:

> Formatting.format (Parser.parse "{default} {nl} {$stringify} {nl} {@capture}") [| foo; nl; foo; nl; foo; |]
val it : string =
  "id => 999, name => foo, created => 11/3/2017"

" "{id = 999;
 name = \"foo\";
 created = 11/3/2017 7:40:16 PM;}" "

" User { id: 999, name: "foo", created: 11/3/2017 7:40:16 PM }"

> System.String.Format("{0}",foo)
val it : string = "id => 999, name => foo, created => 11/3/2017"

@lust4life lust4life changed the title Take advantage of ICustomFormatter or IFormattable Take advantage of ICustomFormatter or IFormattable when an object with default destructuring Nov 4, 2017
@lust4life
Copy link
Author

an object with default destructuring is rendered as a string literal

Is this still true even with support custom format ?

@adamchester
Copy link
Member

Hi @lust4life sorry I haven't got to this yet, I'll try to look in the next few days 👍 Thanks!

@lust4life
Copy link
Author

@adamchester 😃 , ok , take your time. i will ping you after few days.

@lust4life
Copy link
Author

@adamchester do we need consider replace "\n" with "\\n" , when write as string ? right now , we just deal with ". if we also deal with newline, in most case, the output can be just one line string.

type User = 
  {
    id      : int 
    name    : string
    created : DateTime
  }
let foo = { id = 999; name = "foo"; created = DateTime.Now}
Formatting.format (Parser.parse "stringify is: {$foo} and destructure is: {@capture}") [| foo; foo |] 

right now:

val it : string =
  "stringify is: "{id = 999;
 name = \"foo\";
 created = 11/18/2017 2:37:46 PM;}" and destructure is: User { id: 999, name: "foo", created: 11/18/2017 2:37:46 PM }"

replaced:

val it : string =
  "stringify is: "{id = 999;\n name = \"foo\";\n created = 11/18/2017 2:43:30 PM;}" and destructure is: User { id: 999, name: "foo", created: 11/18/2017 2:43:30 PM }"

if user want to keep newline as it was, they can add l as its format.

Formatting.format (Parser.parse "stringify is: {$foo:l} and destructure is: {@capture}") [| foo; foo |] 

val it : string =
  "stringify is: {id = 999;
 name = "foo";
 created = 11/18/2017 2:37:46 PM;} and destructure is: User { id: 999, name: "foo", created: 11/18/2017 2:37:46 PM }"

@lust4life
Copy link
Author

after that, things become as:

  • $ means capture as string (Forcing Stringification), just invoke obj.ToString(), neither ICustomFormatter nor IFormattable will be involved, and this will handle with " and newline.
  • @ mean capture as structured data, try preserving object structure.
  • no prefix mean default strategy, try scalar, try custom,try IEnumerable..., if no case be matched,will capture as origin scalar object, this will try use ICustomFormatter or IFormattable.

@adamchester
Copy link
Member

Hey @lust4life!

do we need consider replace "\n" with "\n" , when write as string ?

Can you please elaborate on why replacing newlines would be better?

@lust4life
Copy link
Author

want to use messagetemplates to format Event message in Logary (maybe next version), seems levelDatetimeMessagePathNewLine did not want to show some newline in message template (the body part), one line will be more conducive to the format unification.

But it's not necessary, it's just a temporary thought and can be done in logary itself. What i really want here is: change scalarStringCatchAllDestr to catch the original scalar object, this will support custom format.

@lust4life
Copy link
Author

and should we support Map here isScalarDict ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants